Exponents Major ?Bug?

  • Thread starter Thread starter Shawn B.
  • Start date Start date
S

Shawn B.

Greetings,

I have the expression that works in Excel, Javascript, and VB.NET:

B1 = 18

10^(3*(B1)/1130)*6

Result: 6.697903112


When I evaluate in C#,


float B1 = 18;
double x;

x = (3*B1/1130) * 6;
x = Math.Pow(x, 10);

I get the wrong result.

Funny, when I add that Math.Pow part to VB.NET and JScript.NET, I get the
same incorrect result.



How can I get the correct result in C#?


Thanks,
Shawn
 
Do we have to mindread to get the WRONG result value in C#?

why mix float and doubles?
 
I would presume, the WRONG result to be anything other than the CORRECT
result. If B1 was instead 18, and I place it in the stead of B1 in the C#
expressions, it will still fail to result in the correct answer
"6.697903112".


Thanks,
Shawn
 
If I cast B1,

float B1 = 18;
double x;
double y=0;

x = (3*(double)B1/1130) * 6;
x = Math.Pow(x, 10);

the incorrect answer would be: 3.75548184850977E-06
which is the same as if I don't cast it. Mind you, if I user Math.Pow(x,
10) in JScript.NET or VB.NET, I get the same incorrect answer. But if I use
the power operators (available in those languages) instead of Math.Pow() I
get the correct answer: 6.697903112


Thanks,
Shawn
 
Shawn said:
Greetings,

I have the expression that works in Excel, Javascript, and VB.NET:

B1 = 18

10^(3*(B1)/1130)*6

Result: 6.697903112


When I evaluate in C#,


float B1 = 18;
double x;

x = (3*B1/1130) * 6;
x = Math.Pow(x, 10);

I get the wrong result.

You get a different result because the expressions are not equivalent.

The C# equivalent of your Excel, JavaScript & VB.NET expression is:

Math.Pow( 10, (3*B1/1130)) * 6

The exponent is (3*B1/1130), which should be the 2nd parameter to
Math.Pow().

Note that 6 is not passed to Math.Pow() - it's not part of the exponent
in your original expression.
 
Shawn,
C# gives you a different answer, as you gave C# a different problem! :-)

Remember that Math.Pow(x, y) returns x ^ y.
float B1 = 18;
double x;

x = (3*B1/1130) * 6;
x = Math.Pow(x, 10);

Asks C# to solve:

((3*(B1)/1130)*6) ^ 10

If you flip the parameters to Math.Pow, you get the answer you expect!
x = Math.Pow(10, x);

Hope this helps
Jay
 
Mike,
Doh! I saw the flipped parameters, I missed the parenthesis.

Shawn, Mike's answer is more correct ;-)

Jay
 
Thank you.


Thanks,
Shawn



mikeb said:
You get a different result because the expressions are not equivalent.

The C# equivalent of your Excel, JavaScript & VB.NET expression is:

Math.Pow( 10, (3*B1/1130)) * 6

The exponent is (3*B1/1130), which should be the 2nd parameter to
Math.Pow().

Note that 6 is not passed to Math.Pow() - it's not part of the exponent
in your original expression.
 
Jack:
Do you get a kick out of spoofing my name and email to be an idiot? Does
that make you feel better to pass yourself off as me? You need to grow up,
and stop using my name and email as yours please.
 
Back
Top