Setting Precision in C++: Floor Rounding to 2 Decimals

  • Context: C/C++ 
  • Thread starter Thread starter FrostScYthe
  • Start date Start date
  • Tags Tags
    C++ Precision
Click For Summary

Discussion Overview

The discussion revolves around achieving floor rounding to two decimal places in C++ during arithmetic operations. Participants explore different methods for maintaining precision in calculations and formatting output, while also debating the implications of rounding at various stages of computation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes a specific calculation and expresses the need to round to two decimal places after each operation.
  • Another participant suggests multiplying by 100, casting to an integer, and then dividing by 100 to achieve the desired rounding effect.
  • A different viewpoint questions the necessity of rounding at each step, arguing that it may lead to loss of precision and suggesting that calculations should be performed with full precision and rounded only at the end.
  • A participant shares a method for printing floating-point numbers with a specified precision using printf, while also expressing a desire to vary the precision dynamically.
  • Another participant recommends using the C++ i/o library for better precision handling but acknowledges the preference for printf in this context.
  • One participant mentions setting precision to 16 digits for double types, indicating a limit on precision that can be achieved.

Areas of Agreement / Disagreement

Participants express differing opinions on the best approach to rounding and maintaining precision, with no consensus reached on whether to round at each step or only at the end of calculations.

Contextual Notes

Some participants highlight the potential loss of precision when rounding at intermediate steps, and there are unresolved considerations regarding the implications of using different formatting methods for output.

FrostScYthe
Messages
80
Reaction score
0
Hey.. I'm having trouble achieving this in C++...

I'm trying to do an opperation but keeping only 2 decimals, and floor rounding.

((3.52 * 4.32)/3.26) + 2.34

So I have 3.52 * 4.32 = 15.2064, but then I just round to 15.20

then 15.20/3.26 = 4.662576687116, but then I just round to 4.66

... and so on...

anyone know how to achive this in C++, I've thought of the floor command but how do I see the precision to only 2 decimals?
 
Technology news on Phys.org
At each intermediate step, you'll need to:

1) Multiply your floating-point numbers by 100.0 (to preserve two decimal places).

2) Cast them to first to integers and then back to floating-point (which will truncate the rest of the decimal places). Alternatively, you can use the floor() function to truncate.

3) Divide the resulting floating-point number by 100.0.

- Warren
 
FrostScYthe said:
Hey.. I'm having trouble achieving this in C++...

I'm trying to do an opperation but keeping only 2 decimals, and floor rounding.

((3.52 * 4.32)/3.26) + 2.34

So I have 3.52 * 4.32 = 15.2064, but then I just round to 15.20

then 15.20/3.26 = 4.662576687116, but then I just round to 4.66

... and so on...

anyone know how to achive this in C++, I've thought of the floor command but how do I see the precision to only 2 decimals?
Why would you ever want to do that anyway? You'll just lose precision. It makes more sense to calculate with all the digits you have and only round at the end.
 
Alright... I have the rounding working now, Thanks so much =).

I'm printing the matrix giving a precision of 5 decimal places... I use

int j, k;
for (j = 0; j < n; j++) {
for (k = 0; k < n + 1; k++) {
printf (" %10.5f ", a[k][j]);
}
printf("\n");
}
printf("\n");

however I want to vary the " %10.5f " to any precision I input... anyone know how to do this... I know there's a way with iomanip.. but I want to use printf because it does it nicer.
 
I would prefer to advise using the C++ i/o library. But, if you insist...

The first argument to printf doesn't have to be a string constant...
 
For more decimal places you could also try putting this in your code

count.precision(16);

You need not use 16, but that(16 digits) is about the limit of you precision, if you are using type double.
 
Last edited:

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
Replies
4
Views
2K
Replies
4
Views
3K
Replies
5
Views
3K
  • · Replies 26 ·
Replies
26
Views
7K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K