Problem using floor() function

This is a discussion on Problem using floor() function within the C Programming forums, part of the General Programming Boards category; I am using the floor function to round the values (round up). Below is the code, all variables are declared ...

  1. #1
    Unregistered
    Guest

    Question Problem using floor() function

    I am using the floor function to round the values (round up).

    Below is the code, all variables are declared as double
    OS: HP-Unix 11

    dTemp = dValueToRound * dMultFactor;
    printf("dTemp: %f\n", dTemp);
    dTemp = floor (dTemp + 0.5);
    dRoundedVal = dTemp / dMultFactor;

    printf("Rounded value: %f\n", dRoundedVal);

    When I try to round the number 1.275 using the above function it is rounding it down to 1.27

    If I directly pass the the value 128.00 to the floor function it returns 128.00 but if I do this floor(1.275*100.00 + 0.5) it returns 127.00. I did not understand whether I am missing something.


    Help would be appreciated.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    If you want to round up then perhaps the ceil function would work a bit better. Or you could search the boards for previous times this question has been asked.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21