How am I incorrectly using the round() function in C?

后端 未结 7 1522
-上瘾入骨i
-上瘾入骨i 2021-02-08 21:42

I\'m getting unexpected results from the round() and roundf() functions in the math.h library. Here is the sample code:

#         


        
7条回答
  •  醉话见心
    2021-02-08 22:13

    To add to the chorus of approvals, this works fine for me (OS X Leopard using gcc).

    Out of curiosity, do you need to use the -lm flag? I didn't the first time, and I did the second time, and there was no difference, but that's because libm.dylib and libc.dylib are symbolic links to the same library on OS X. Perhaps your libm is broken or something? Do you need to link to libm or not? I would think that the math.h functions would be part of libc...?

    EDIT:

    Before you do any of that, try using this instead:

    float f = 0;
    double d = 0;
    

    I don't think that should change anything, but if it does, I win.

提交回复
热议问题