Howdy, Stranger!

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

Categories

problems w/ pow() function

brtkltbrtklt Member Posts: 237
When I use pow(), I'm getting results that aren't close to right. I know pow() requires a floating point number and am using one but am still getting wrong results. The following code works:

double i = 4;
double j;
j = (2,i)

j will have 16.

But if I use a larger number (I'm trying to get 21 to the power of 2) it won't work correctly.

double i = 21;
double j;
j = pow(2,i);

j now holds 2.09715e+06

I'm not clear on why this is happening. Am I using the wrong data type? How do I get the right value out of this ( j = 441 )?

Comments

  • DariusDarius Member Posts: 1,666
    : When I use pow(), I'm getting results that aren't close to right. I know pow() requires a floating point number and am using one but am still getting wrong results. The following code works:
    :
    : double i = 4;
    : double j;
    : j = (2,i)
    :
    : j will have 16.
    :
    : But if I use a larger number (I'm trying to get 21 to the power of 2) it won't work correctly.
    :
    : double i = 21;
    : double j;
    : j = pow(2,i);
    :
    : j now holds 2.09715e+06
    :
    : I'm not clear on why this is happening. Am I using the wrong data type? How do I get the right value out of this ( j = 441 )?
    :

    This should be obvious, pow(x,y) is x^y, it reads in order (why do you think it's y^x?) Anyways, you should easily be able to tell that instead of 21^2 you are getting 2^21 which is 2 million some as it says.

    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

  • brtkltbrtklt Member Posts: 237
    Simple enough. Thanks.
    : : When I use pow(), I'm getting results that aren't close to right. I know pow() requires a floating point number and am using one but am still getting wrong results. The following code works:
    : :
    : : double i = 4;
    : : double j;
    : : j = (2,i)
    : :
    : : j will have 16.
    : :
    : : But if I use a larger number (I'm trying to get 21 to the power of 2) it won't work correctly.
    : :
    : : double i = 21;
    : : double j;
    : : j = pow(2,i);
    : :
    : : j now holds 2.09715e+06
    : :
    : : I'm not clear on why this is happening. Am I using the wrong data type? How do I get the right value out of this ( j = 441 )?
    : :
    :
    : This should be obvious, pow(x,y) is x^y, it reads in order (why do you think it's y^x?) Anyways, you should easily be able to tell that instead of 21^2 you are getting 2^21 which is 2 million some as it says.
    :
    : "We can't do nothing and think someone else will make it right."
    : -Kyoto Now, Bad Religion
    :
    :

  • Justin BibJustin Bib USAMember Posts: 0

    \ http://forcoder.org \ free ebooks and video tutorials about \ Scratch, C#, Java, PHP, Objective-C, JavaScript, Delphi, Visual Basic .NET, C, Perl, Swift, Assembly, MATLAB, C++, R, Ruby, Go, Visual Basic, Python, PL/SQL Fortran, ABAP, SAS, Rust, Hack, Julia, Ada, Clojure, Kotlin, Awk, Dart, F#, Erlang, Scala, LabVIEW, Scheme, Prolog, FoxPro, Apex, Transact-SQL, D, Crystal, Bash, ML, Lua, Alice, COBOL, Lisp, VBScript, Logo \ __________

Sign In or Register to comment.