C - Rounding issues (CS50)

前端 未结 2 863
旧巷少年郎
旧巷少年郎 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: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
    

提交回复
热议问题