1. Round A Negative Number

This is quite an obscure bit of trivia.

-4.001 at 2 decimal place = -3.99
-1.001 at 2 decimal place = -0.99
-0.001 at 2 decimal place = ?

The answer is 0.00, since the difference between 399 and 400 is 1, same for 99 and 100. The difference between 001 is 000 is also 1.

It is difficult for someone to digest the fact that a number is reduced to nothing.

2. How can a single short post be so wrong?

Tim S.

4. This is not a website that does your homework!

Tim S.

5. You do realize floating point is not "decimal" and not accurate enough to represent 0.001, don't you?

Floating point is a fractional representation using 3 unsigned integer values: S, F and E, where the value v is represented as:

Where p is the precision, in bits (24 for float), so F is 23 bits long; S is always 1 bit and E, for simplicity, here, is a signed 8 bits value (but is coded as E-127, in a float, where E is unsigned).

4.001 is F=2097, E=2 (1 ulp = 2⁻²¹)
1.001 is F=8389, E=0 (1 ulp = 2⁻²³)
0.001 is F=201327, E=-10 (1 ulp = 2⁻¹³)

ULP (Unit at Last Position) is the actual increment (as a fraction) due to increment of F, after the scaling.

That's how floating point works... Now, if you want to round the decimal value to the nearest taking only 3 decimal places in consideration you can do:

Code:
`printf( "%.3f", v );`
And you'll end up with -4.001, -1.001 and -0.001 printed.

-4.001, for example, is -4.000999927520751953125 and printf will correctly round this to -4.001.

Don't trust my words, do it yourself:
Code:
```float f = -4.001f;
printf( "%.28f -> %.3f\n", f, f );```
But there is no way you can represent, exactly, fractions (in positional notation) not ending in 5 (and some of them too. Example: 0.15 isn't exact).

6. I am not using printf or printf method of rounding.
Does -0.001 "rounded-down" become 0.00, that's all I want to know.

7. Originally Posted by ZTik
I am not using printf or printf method of rounding.
Does -0.001 "rounded-down" become 0.00, that's all I want to know.
You didn't read, did you? Floating point isn't decimal!

8. I am not using a float or a double.

9. Then it is a "practical" rounding, as in high school?
-0.001 is closer to 0.00 or -0.01?

10. That is not practical rounding from high school.

11. There are 4 directions of rounding.

Round towards + infinity.
Round towards - infinity.
Round towards zero.
Round away from zero.

Tim S.

12. Originally Posted by stahta01
There are 4 directions of rounding.

Round towards + infinity.
Round towards - infinity.
Round towards zero.
Round away from zero.

Tim S.
Some rounds to closest integer and uses one of the 4 directions if it is at the exact midway point.

Tim S.