SimpleIDE ver 0.9.27 float problem
Iamretired
Posts: 56
I am using the tutorial to learn C,
I have tried to calc a ratio and get no result. I have a similar problem with mod,
#include "simpletools.h"
#include "math.h"
int main()
{
pause(1000);
float x = 3;
float y = 4;
float ratio;
ratio = y / x;
printf("ratio is %f\n", ratio);
}
On the screen I get ratio is with no calc result.
Johnmb
I have tried to calc a ratio and get no result. I have a similar problem with mod,
#include "simpletools.h"
#include "math.h"
int main()
{
pause(1000);
float x = 3;
float y = 4;
float ratio;
ratio = y / x;
printf("ratio is %f\n", ratio);
}
On the screen I get ratio is with no calc result.
Johnmb
Comments
This thread has more information: http://forums.parallax.com/showthread.php/136586-did-I-miss-a-note-on-floats-in-printf
Johnmb
It is a Compiler option, but may actually not be hooked-up at this time and may be ignored. But, yes the Linker option "-lm" should work, no matter.
A Mac OS X view of the Compiler options... Looks almost the same on Windows.
dgately
This program works.
#include "simpletools.h"
int main() // Main function
{
pause(1000); // Wait 1 s for host computer
float r = 1;
float x = 4;
float y = 3;
float ratio = x / y;
float v = ratio* PI * r * r * r;
printf("ratio = %f \n", ratio);
printf("volume = %f \n", v); // Display volume
}
ratio = 1.333333
volume = 4.188790
The following form doesn’t work and I don’t know why.
#include "simpletools.h" // Include simpletools
int main() // Main function
{
pause(1000); // Wait 1 s for host computer
float r = 1;
float ratio = 4/3;
float v = ratio* PI * r * r * r; // replaced ratio with 4/3 and result same
printf("ratio = %f \n", ratio);
printf("volume = %f \n", v); // Display volume
}
ratio = 1.000000
volume = 3.141593
__learn.parallax.com/propeller-c-start-simple/floating-point-math
It shows how to check the Math Lib box in Show Project Manager -> Linker Tab -> Math Lib.
Also, to make sure it wasn't something else, I saved a copy of the example program from:
\Documents\SimpleIDE\Learn\Examples\C Intro\Basics\Floating Point Calculations.side
...to \Documents\SimpleIDE\My Projects, then used Run with Terminal to see the results. It worked:
Whenever you use floating point, tell the IDE to link the math library by using the Math Lib check-box shown here. (Show Project Manager button is in bottom-left corner.)
We left it unchecked in the New Project Template so that programs with integer math would not carry the extra weight. (We might want to rethink that.)
Andy
Yes. And we should encourage them to use scaling and fractions instead of float/double, if you want my opinion. I've never encountered a situation where floating point (with all its pitfalls such as non-representable numbers, inaccuracies, comparing for equality etc) couldn't be avoided.
===Jac
Johnmb