using round from math.h

This is a discussion on using round from math.h within the C Programming forums, part of the General Programming Boards category; Hi, I need to round a number. I tried using round from math.h but it's returning weird values Code: double ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    205

    using round from math.h

    Hi,
    I need to round a number. I tried using round from math.h but it's returning weird values
    Code:
     double num1 = 0;
    num1 = round(2.67);
     printf("col = %f\n", num1);
    I am getting -524288.000000 ?????

    Anybody can help with this
    Amish

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Are using using a C99 implementation?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Perhaps you didn't include the right header, and it implicitly declared it as returning int.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    I used #include <math.h>

    and when i compile I added the flag -lm

    I dunno what the c99 thingy is. Can someone explain more. Thanks
    Amish

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    C99 is the new C standard.
    round is a new function in C99.

    Which OS/Compiler are you using?
    What compiler options did you use?
    Did you get any warnings?

    $ gcc -std=c99 foo.c -lm
    $ ./a.out
    col = 3.000000
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    sorry for the late reply. I do not get any warnings using round without the -c99 flag. I finally created my own round function. But thanks for the info.
    Amish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. Fibonacci Formula, How to round in C++?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 10-15-2004, 10:47 AM
  3. I need some quick help with code.
    By stehigs321 in forum C Programming
    Replies: 35
    Last Post: 10-30-2003, 09:07 PM
  4. Someone help me with this game??
    By stehigs321 in forum Game Programming
    Replies: 15
    Last Post: 10-30-2003, 08:42 PM
  5. Please help with some coding..
    By stehigs321 in forum C Programming
    Replies: 2
    Last Post: 10-27-2003, 05:44 PM

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