C - Rounding issues (CS50)

前端 未结 2 862
旧巷少年郎
旧巷少年郎 2021-01-26 06:06

I have been Googling this for days now and I am lost. So doing CS50 online and can\'t seem to get a handle on this rounding of numbers. My program is messing up multiplying floa

相关标签:
2条回答
  • 2021-01-26 06:38

    I have seen the posts about -lm and a certain file but if I am honest I don't understand what it means.

    You have to link to the math library to fix the error. Math functions implementations are usually put as a separate library, the math library. If you use gcc add -lm to the linker command.

    0 讨论(0)
  • 2021-01-26 06:49

    I presume you want to round floating numbers to the nearest integer. ceilf is not doing that, it is rounding up. You can use this macro for rounding to nearest long when possible:

    #define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
    

    Also, you get a linking error because you don't link with the math library. For example use :

    gcc greedy.c -lm -o greedy
    
    0 讨论(0)
提交回复
热议问题