Prop C Math Question
Blake Koch
Posts: 39
I am starting to learn c language for Propeller. In the first section of the tutorial in section (Floating Point Math) at the bottom in the try this section the task is to multiply floating decimal points. a simple volume equation of (float v=4/3*PI*r*r*r) then print out the answer in the IDE terminal. When I brake the formula down to just (float v= 4/3) my ide screen reads 1.000000. It should read 1.3333333. If I times 4/3 * PI I read on the ide 3.141593. YET when I change the formula around PI * 4/3 I get the right answer 4.188790. WHY does 4/3 equal 1.000000 What am I not doing right.
thanks
thanks
Comments
It is seeing 4/3 as two integers 4 and 3 which when you divide you get 1 because there is no decimal result for integers.
When entering formulas for floating point calculatiions enter floating point values for constants
Ex 4.0 / 3.0 then the compiler will treat it as a float within the expression
When you did PI*4/3 the Pi*4 partial expression is a float times an int and is a float with some decimal places in the result
which when divided by 3 has decimal places in the final result
Tom
It always seems to be something so simple.
Blake
http://forums.parallax.com/showthread.php/134682-Forum-Guidelines
For example, what is the output of the following program?
This is normally not a worry, until the things in your expression start to have side effects. Like that call to wtf() above;