Shop OBEX P1 Docs P2 Docs Learn Events
Truncating in SimpleIDE — Parallax Forums

Truncating in SimpleIDE

Not sure if this is the correct place to post this, if not please let me know!
I'm trying to use trunc() in SimpleIDE to round to a few decimal places, but the compiler tells me, "bad function call 'trunc', as if it doesn't recognize the function. But I know trunc is working because if I put, say, trunc(2.44) in, it compiles just fine.
The puzzling thing is that if I change _time_sweep
's data type from double to int, it compiles just fine- as if the double type cast didn't happen, and it's treating test as an int. Can anyone give me a clue? I have been stuck on this for some time now.

Comments

  • Try adding an #include <math.h>.

  • I have tried this (it was what my professor recommended I do first as well), but it changed nothing. Also, doesn't "simpletools.h" include "math.h"?

  • I suspect that the trunc() function isn't in the library. trunc(2.44) works because the compiler evaluates this to 2.0 at compile time without adding a call to trunc() at runtime. You could use (double)(int)x, which would be the same as trunc(x) as long as the integer part fits within a 32-bit integer.

    I see that the only place you use the trunc() function is in the line "time_sweep = trunc((double)theta);". Since theta is an int, it is already truncated to an integer. You would get the same result with "time_sweep = (double)theta;".

Sign In or Register to comment.