[C++] My first IT course with simple output

  • Context: C/C++ 
  • Thread starter Thread starter Silicon Waffle
  • Start date Start date
  • Tags Tags
    Course Output
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
9 replies · 2K views
Silicon Waffle
Messages
160
Reaction score
202
Hi, my C++ course has started since last week and I am learning basic input and output.
In my simple project, I have a function that will return a double number like this
Code:
double getLong(long long ll)
{
    return ll/10.00;
}

The input is 12345 and the return value after the code is executed is 1234.500000
I find it strange as to why 0's are added unexpectedly.
I create a console application and test again with main like this
Code:
int main()
{
    count<<12345/10.00;
}
But the result becomes 1234.5 which is correct.

Another case I run with my function is if I input e.g 1234567890 then I expect the result should be 123456789.0 (I don't want the compiler to automatically delete my 0 after the decimal point). How can I achieve this ? Thank you a lot.
 
Physics news on Phys.org
I see the values in the debugger.
By the way I fixed it using the tutorial link in jedishrfu's post. I learned new thing in C++, setw and set precision. My teacher seemingly forgot them in his previous lecture.
Thanks.
 
Silicon Waffle said:
I find it strange as to why 0's are added unexpectedly.
Silicon Waffle said:
I see the values in the debugger.

The (default) number of decimal places to show is a decision by the authors of the debugger. There might be an option in the debugger's preferences section to change this.
 
  • Like
Likes   Reactions: Silicon Waffle
jtbell said:
The (default) number of decimal places to show is a decision by the authors of the debugger. There might be an option in the debugger's preferences section to change this.
Really ? I don't know if there is.
 
How exactly is the numerical value 1234.500000 any less correct than 1234.5? Or 1234.500 for that matter?

The output of your program (the text string "1234.5") is what is important and that is correct.

BoB
 
rbelli1 said:
How exactly is the numerical value 1234.500000 any less correct than 1234.5? Or 1234.500 for that matter?

The output of your program (the text string "1234.5") is what is important and that is correct.

BoB
Thank you BoB for your insight. Yes, it should be that.
I'm working on a financial application so I need to read from and write to the file the exact values.
 
Silicon Waffle said:
Thank you BoB for your insight. Yes, it should be that.
I'm working on a financial application so I need to read from and write to the file the exact values.
You need to be very careful when using floating point to represent money.
Code:
#include <iostream>
int main ()
{
   double x = 0.10;
   double y = 0.20;
   double z = 0.30;
   if ((x+y) != z)
   {  
      std::cout << "This doesn't seem right!\n"
                << x << " + " << y << " - " << z << " = " << x+y-z << '\n';
   }  
}
On most computers, this will print
Code:
This doesn't seem right!
0.1 + 0.2 - 0.3 = <some small but non-zero number>
 
Last edited:
  • Like
Likes   Reactions: Silicon Waffle
The best way to be careful with floating point and currency is not to use floating point for currency.

You can use integers and count cents, or you can look at fixed precision or decimal libraries. If you can't find anything better, there's cpp_dec_float in boost. I believe that in commercial code some juristictions require the use of decimal libraries for currency.
 
  • Like
Likes   Reactions: Silicon Waffle