Thread: Round A Negative Number

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    17

    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.
    Last edited by ZTik; 05-10-2021 at 07:51 AM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    How can a single short post be so wrong?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2021
    Posts
    17
    Please, elaborate..

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This is not a website that does your homework!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    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:

    Round A Negative Number-untitled-png

    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).
    Last edited by flp1969; 05-10-2021 at 10:00 AM.

  6. #6
    Registered User
    Join Date
    May 2021
    Posts
    17
    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. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by ZTik View Post
    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. #8
    Registered User
    Join Date
    May 2021
    Posts
    17
    I am not using a float or a double.

  9. #9
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Then it is a "practical" rounding, as in high school?
    -0.001 is closer to 0.00 or -0.01?

  10. #10
    Registered User
    Join Date
    May 2021
    Posts
    17
    That is not practical rounding from high school.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    There are 4 directions of rounding.

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

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by stahta01 View Post
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. round a number to superior integer
    By nevrax in forum C Programming
    Replies: 5
    Last Post: 03-30-2007, 02:57 AM
  2. How can i convert negative number to positive number ?
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 05-05-2004, 08:02 AM
  3. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM
  4. how to read in a negative number using cin
    By revelation437 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2003, 02:32 PM

Tags for this Thread