[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
Click For Summary

Discussion Overview

The discussion revolves around issues related to output formatting in C++, particularly concerning the representation of floating-point numbers and their precision. Participants explore how to control the display of trailing zeros and the implications of using floating-point arithmetic in financial applications.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes a function that returns a double and notes unexpected trailing zeros in the output.
  • Another participant suggests that the default printing format in C++ may suppress trailing zeros and provides a link to adjust number formatting.
  • A question is raised about how the return value is known, prompting a clarification that values can be seen in the debugger.
  • One participant expresses confusion about the correctness of different numerical representations (e.g., 1234.500000 vs. 1234.5).
  • A participant mentions working on a financial application and emphasizes the importance of exact values when reading from and writing to files.
  • Concerns are raised about the reliability of floating-point arithmetic for representing money, with an example provided that illustrates potential inaccuracies.
  • Another participant recommends avoiding floating-point types for currency, suggesting the use of integers or specialized libraries for fixed precision.

Areas of Agreement / Disagreement

Participants express differing views on the significance of trailing zeros in numerical output and the appropriateness of using floating-point numbers for financial calculations. There is no consensus on the best approach to handle these issues.

Contextual Notes

Participants highlight limitations in the default behavior of C++ output formatting and the potential pitfalls of using floating-point arithmetic for financial applications, but do not resolve these concerns.

Who May Find This Useful

Individuals learning C++ programming, particularly those interested in input/output formatting and financial applications involving numerical precision.

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.
 
Technology news on Phys.org
Silicon Waffle said:
The input is 12345 and the return value after the code is executed is 1234.500000

How do you know what the return value is?
 
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
  • #10
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

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K