T
Torben Frandsen
Hi
I'm porting an old Access/VB application to .Net, and I've encountered a
small problem.
The VB application takes a value from a query and stores it in a Double.
This value is the result of a calculation involving three values all stored
in Access as Single. This is how the values bubble up:
Bottom query
(Foo*Bar) as Baz
Middle query
Sum(Baz) as Quux
Top query
(Quux * Gazonk) as Gloop
Let's put some real world numbers in there:
Foo = 100.2
Bar = .210666
(Only one row for the middle query)
Gazonk = .001
Access produces the result 0.021108731318773.
Before you warm up to lecture me on floating point arithmetics, let's see
what C# makes of it:
First, let's just use float/Single:
?.210666f*100.2f*.001f
0.0211087335
I get an error, but not the error I was looking for. Perhaps Access casts to
a double?
?(double).210666f*100.2f*.001f
0.021108733644182805
Nope, the error is moving in the wrong direction. Does it cast everything to
double and then do the math?
?.210666d*100.2d*.001d
0.0211087332
Whaddaya know! This is the mathematically correct result. Unfortunately, I'd
like the old well-known error. So does it cast everything to double and then
jam the result back into a float/Single?
?(float).210666d*100.2d*.001d
0.021108733284473422
Guess not. So, what is going on? Is there any way for me in C# to reproduce
the results from Access?
(And yes, it does matter. When you pour numbers like these into a 95x95
linear equation system, the errors scale up, and it becomes close to
impossible to compare the results.)
Thanks,
Torben
I'm porting an old Access/VB application to .Net, and I've encountered a
small problem.
The VB application takes a value from a query and stores it in a Double.
This value is the result of a calculation involving three values all stored
in Access as Single. This is how the values bubble up:
Bottom query
(Foo*Bar) as Baz
Middle query
Sum(Baz) as Quux
Top query
(Quux * Gazonk) as Gloop
Let's put some real world numbers in there:
Foo = 100.2
Bar = .210666
(Only one row for the middle query)
Gazonk = .001
Access produces the result 0.021108731318773.
Before you warm up to lecture me on floating point arithmetics, let's see
what C# makes of it:
First, let's just use float/Single:
?.210666f*100.2f*.001f
0.0211087335
I get an error, but not the error I was looking for. Perhaps Access casts to
a double?
?(double).210666f*100.2f*.001f
0.021108733644182805
Nope, the error is moving in the wrong direction. Does it cast everything to
double and then do the math?
?.210666d*100.2d*.001d
0.0211087332
Whaddaya know! This is the mathematically correct result. Unfortunately, I'd
like the old well-known error. So does it cast everything to double and then
jam the result back into a float/Single?
?(float).210666d*100.2d*.001d
0.021108733284473422
Guess not. So, what is going on? Is there any way for me in C# to reproduce
the results from Access?
(And yes, it does matter. When you pour numbers like these into a 95x95
linear equation system, the errors scale up, and it becomes close to
impossible to compare the results.)
Thanks,
Torben