why isnt my formula workn in c++


sideB is equal to 4 by the way....

area=(4/3*3.14*(sideB*sideB*sideB));

its out putting 200.96 when it should be 267.946 whats wrong with this or any ideas???

Comments

  • The only thing I can see programatically wrong with this is that 4 / 3 is going to return 1, the floating point should read 4.0 / 3.0 for accuracy.

    :
    : sideB is equal to 4 by the way....
    :
    : area=(4/3*3.14*(sideB*sideB*sideB));
    :
    : its out putting 200.96 when it should be 267.946 whats wrong with this or any ideas???
    :

  • area==(4/3*3.14*(sideB*sideB*sideB)) <==>
    200.96==4/3*3.14*4*4*4 <==>
    200==1/3*3.14*16 <==>
    1==200*48*3.14 (?????????????????)
    : The only thing I can see programatically wrong with this is that 4 / 3 is going to return 1, the floating point should read 4.0 / 3.0 for accuracy.
    :
    : :
    : : sideB is equal to 4 by the way....
    : :
    : : area=(4/3*3.14*(sideB*sideB*sideB));
    : :
    : : its out putting 200.96 when it should be 267.946 whats wrong with this or any ideas???
    : :
    :
    :

  • What are you trying to work out there? As was previously said, the only problem is the 4/3 needs to be replaced with 4.0/3.0. (because one equates to 1, the other 1.3 repetetive)

    : area==(4/3*3.14*(sideB*sideB*sideB)) <==>
    : 200.96==4/3*3.14*4*4*4 <==>
    : 200==1/3*3.14*16 <==>
    : 1==200*48*3.14 (?????????????????)
    :
    : : The only thing I can see programatically wrong with this is that 4 / 3 is going to return 1, the floating point should read 4.0 / 3.0 for accuracy.
    : :
    : : :
    : : : sideB is equal to 4 by the way....
    : : :
    : : : area=(4/3*3.14*(sideB*sideB*sideB));
    : : :
    : : : its out putting 200.96 when it should be 267.946 whats wrong with this or any ideas???
    : : :
    : :
    : :
    :
    :

  • :
    : sideB is equal to 4 by the way....
    :
    : area=(4/3*3.14*(sideB*sideB*sideB));
    :
    : its out putting 200.96 when it should be 267.946 whats wrong with this or any ideas???
    :

    dwgebler is right but I'd add something: when you have several
    constants in an arithmetic expression, you may want to replace them
    by a single value - the result of that constant expression (normally the compiler does that for you -if it's possible- but you never know for sure).

    For example, 4*PI/3 is almost equal to 4.18879, so you can write your
    code as this:
    [code]
    area = 4.18879 * sideB * sideB * sideB;
    [/code]

    Regards,
    Blitz

  • :
    : sideB is equal to 4 by the way....
    :
    : area=(4/3*3.14*(sideB*sideB*sideB));
    :
    : its out putting 200.96 when it should be 267.946 whats wrong with this or any ideas???
    :
    [blue]
    area=(4/3*3.14*(sideB*sideB*sideB));

    area = (4 / 3 * 3.14 * (4 * 4 * 4))
    area = (4 / 3 * 3.14 * (4 * 16))
    area = 4 / 3 * 3.14 * 64
    area = 1 * 3.14 * 64
    area = 3.14 * 64
    area = 3.14 * 64
    area = 200.96
    The integer division is enough to mess you up.

    With float division:
    area = (4.0 / 3 * 3.14 * (4 * 4 * 4))
    area = 4.0 / 3 * 3.14 * 64
    area = 1.333 * 3.14 * 64
    area = 1.333 * 3.14 * 64
    area = 4.18562 * 64
    area = 267.87968
    These things seem tiny, but can really come and bite you if you are not specific with the compiler.
    [/blue]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories