Shop OBEX P1 Docs P2 Docs Learn Events
SimpleIDE ver 0.9.27 float problem — Parallax Forums

SimpleIDE ver 0.9.27 float problem

IamretiredIamretired Posts: 56
edited 2013-05-09 11:30 in Propeller 1
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

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2013-05-08 13:05
    simple printf doesn't support floating point. Uncheck the simple printf option and it should work.
  • SRLMSRLM Posts: 5,045
    edited 2013-05-08 13:10
    You need to link with the -lm option. Here is the code that I used (I don't have simpletools):
    #include <math.h> 
    #include <stdio.h>
    int main() 
    {
    	float x = 3;
    	float y = 4;
    	float ratio;
    	ratio = y / x;
    	printf("ratio is %f\n", ratio); 
    }
    
    user@desktop:~/Downloads$ propeller-elf-gcc -o float_main.elf float_main.c -lm
    user@desktop:~/Downloads$ propeller-load -r -t float_main.elf 
    Propeller Version 1 on /dev/ttyUSB0
    Loading float_main.elf to hub memory
    21976 bytes sent                  
    Verifying RAM ... OK
    [ Entering terminal mode. Type ESC or Control-C to exit. ]
    ratio is 1.333333
    

    This thread has more information: http://forums.parallax.com/showthread.php/136586-did-I-miss-a-note-on-floats-in-printf
  • IamretiredIamretired Posts: 56
    edited 2013-05-08 14:15
    Where is the "simple printf option" ?

    Johnmb
  • dgatelydgately Posts: 1,630
    edited 2013-05-08 14:28
    Iamretired wrote: »
    Where is the "simple printf option" ?

    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.

    simplePrintf.png



    dgately
    328 x 176 - 21K
  • IamretiredIamretired Posts: 56
    edited 2013-05-08 17:35
    I tried several ways including the tutorial exercise to calc volume.

    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
  • kuronekokuroneko Posts: 3,623
    edited 2013-05-08 17:40
    4/3 is seen as an integer expression and treated as such. Try 4.0/3.0 or 4.0f/3.0f (the former is double IIRC).
  • edited 2013-05-08 19:22
    Here is a tutorial page on floating point:

    __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:

    attachment.php?attachmentid=101506&d=1368065815


    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.)

    attachment.php?attachmentid=101507&d=1368066082

    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
    892 x 436 - 293K
    376 x 463 - 211K
  • David BetzDavid Betz Posts: 14,516
    edited 2013-05-09 05:32
    #include "math.h"
    
    Shouldn't we be encouraging people to use angle brackets to include system header files like math.h?
  • jac_goudsmitjac_goudsmit Posts: 418
    edited 2013-05-09 09:09
    David Betz wrote: »
    #include "math.h"
    
    Shouldn't we be encouraging people to use angle brackets to include system header files like math.h?

    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
  • Dave HeinDave Hein Posts: 6,347
    edited 2013-05-09 09:38
    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.
    I wrote an orbital simulator that uses double. It would be extremely cumbersome to implement with integers. Of course, it currently doesn't run on the Prop, but P2 may be able to run it.
  • IamretiredIamretired Posts: 56
    edited 2013-05-09 11:30
    I thank you all for such 'to the point' and helpful replies. I am enjoying the tutorial.

    Johnmb
Sign In or Register to comment.