Shop OBEX P1 Docs P2 Docs Learn Events
Prop C Math Question — Parallax Forums

Prop C Math Question

Blake KochBlake Koch Posts: 39
edited 2013-11-06 00:06 in Propeller 1
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

Comments

  • tdlivingstdlivings Posts: 437
    edited 2013-11-05 21:00
    Blake
    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
  • Mike GreenMike Green Posts: 23,101
    edited 2013-11-05 21:01
    The various operations are performed based on their priority (precedence actually) with * and / being of the same precedence. So, for example, * is evaluated before + unless you use parentheses to explicitly specify which operation is to be done first. For a sequence of operations of the same precedence, they're done left to right. The formula given, v = 4/3 * PI * r * r * r is understood as v = ((((4/3)*PI)*r)*r)*r. A number without a decimal is considered to be an integer. In your case, 4/3 indicates that the integer 4 and the integer 3 are to be divided using integer division with the result being an integer. The result is 1. If you were to put a decimal point on one or both of those numbers, the division would be done as a floating point operation giving a result of 1.333333.
  • Blake KochBlake Koch Posts: 39
    edited 2013-11-05 21:07
    Thank you guys I now see what's going on.
    It always seems to be something so simple.

    Blake
  • davejamesdavejames Posts: 4,047
    edited 2013-11-05 22:12
    Blake - the double post in the Prop2 forum is a no-no according to Forum Guidelines and has been removed.

    http://forums.parallax.com/showthread.php/134682-Forum-Guidelines
  • Heater.Heater. Posts: 21,230
    edited 2013-11-06 00:06
    Mike,
    For a sequence of operations of the same precedence, they're done left to right.
    Actually this is not totally true. C/C++ does not define the order of evaluation in many cases. The rules about this are bit complicated.

    For example, what is the output of the following program?
    #include <stdio.h>
    
    int wtf(int *k) { return *k += 5; }
    
    int main () {
        int k = 10;
        int w = k + wtf (&k);
        printf ("%d\n", w);
        return 0;
    }
    

    This is normally not a worry, until the things in your expression start to have side effects. Like that call to wtf() above;
Sign In or Register to comment.