Bad math when using * operator along with Math.pow
From: webmaster_at_nahnet.com ((webmaster_at_nahnet.com))
Date: 08/14/04
- Next message: Ianier Munoz: "Re: Bad math when using * operator along with Math.pow"
- Previous message: Joshua Drake: "JBImp"
- Next in thread: Ianier Munoz: "Re: Bad math when using * operator along with Math.pow"
- Reply: Ianier Munoz: "Re: Bad math when using * operator along with Math.pow"
- Reply: Jay B. Harlow [MVP - Outlook]: "Re: Bad math when using * operator along with Math.pow"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 14 Aug 2004 00:19:06 -0700
Check out this code. I need to be able to multiply a 15 digit decimal number by 10 to the power of 15, and come up with the correct answer. Easy enough for other languages to do, but J# seems to choke on it. Here's the code.
private void button1_Click (Object sender, System.EventArgs e)
{
double dblPi = 3.14159265358979;
double dblTheta = 121;
double value = System.Math.Sin( (dblTheta / 180) * dblPi );
System.Console.Write(value);
System.Console.Write("\n");
System.Console.Write(Math.pow(10, 15) * value);
// Should return 857167300702113
// but instead returns 857167300702114
}
My best guess is that it has something to do with the * operator. My guess is that it does some kind of crazy rounding, but looking at the code it doesn't make sense really. I made sure that the original value comes out right, and it does, and I trust the Math.pow function to come out right as well, but the combination of it all fails.
I'm so puzzled. HELP!
- Nick
**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
- Next message: Ianier Munoz: "Re: Bad math when using * operator along with Math.pow"
- Previous message: Joshua Drake: "JBImp"
- Next in thread: Ianier Munoz: "Re: Bad math when using * operator along with Math.pow"
- Reply: Ianier Munoz: "Re: Bad math when using * operator along with Math.pow"
- Reply: Jay B. Harlow [MVP - Outlook]: "Re: Bad math when using * operator along with Math.pow"
- Messages sorted by: [ date ] [ thread ]