Click here to Skip to main content
15,898,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Morning,

I developed a Vc++ DLL called by a VB program. The DLL has a function that does some operation on vectors of points(double[3])

now suppose we have two set of point S , T.

If The Vb program calls the function only one time with set of points T i get a result.
If the Vb program calls the function two time, first with set S and then with set T, the result for the set T is different from the result before.

I try to debug and the difference is a product of two double that act differently.
I have two double dx and dy and the function does:

dx2 = dx*dx
dy2 = dy*dy

with the debug strings i get:

if i call only Set T i Get
[2920]point 10-->   dx: 32951.71011630935400000000 dy: 1784.60091076856500000000 dx2: 1085815199.58928420000000000000 dy2:3184800.41071599160000000000 A:1089000000.00000020000000000000   B:0.00065903420232618713  C:-272250000.00000000000000000000  



with the call with set T after the call with set S:
[2920]<pre>point 10-->   dx: 32951.71011630935400000000 dy: 1784.60091076856500000000 dx2: 1085815199.58928390000000000000 dy2:3184800.41071599160000000000 A:1089000000.00000000000000000000   B:0.00065903420232618702  C:-272250000.00000000000000000000  



as you can see dx is the same for both the calls, but the resut dx2 is different.
in final result the difference accumulates for all the points and i get slighlty different value.

Does anybody have an idea of what is happening here?
It seems the after the first call the function is in a different state but how this can affect a product with same operands?
thank you

What I have tried:

I tried to check for memory leaking but i didn't see nothing odd.
Posted
Updated 5-Jun-17 21:12pm
Comments
Kornfeld Eliyahu Peter 4-Jun-17 6:28am    
You may have some global variables to store state?
Looking at your code may help us to help you...
Richard MacCutchan 4-Jun-17 11:54am    
Without seeing your code it is difficult to guess what is happening.

A double will not give you infinite precision - ever. This points to why math calculation using C++, wrong answers - Stack Overflow[^]
32951.71011630935400000000 can probably not be represented in binary so there is a problem to begin with, neither can the result.
Do the calculation here High precision calculator[^] and you will get 1085815199.589284220118545827897316
Why your code is behaving in this particular way can only be determined by observing the flow of execution.
If you wish to use very high precision in numerical calculations you will need to look at something like The GNU MP Bignum Library[^]
 
Share this answer
 
v2
It is a common issue that double values can slightly differ depending on the operation flow. Are you sure that the input is really the same? Normally you avoid that problem by allowing some differences.

Any mathematical computation is better done in the faster and more preciser C++ language.
Else you must think about using different presentation of your values.

Use the option "Strict" in VB.
 
Share this answer
 
v2
Comments
[no name] 4-Jun-17 13:52pm    
FYI: Did a minor correction issuerthat v.s issue that ;)
[no name] 4-Jun-17 21:22pm    
The OP is already calling a function in a c++ dll to do the calculation.
Well, to make it work i had to call a _fpreset() at the beginning of the function.
Something in the component that calls my library changes the state of the floating point unit, but this is still strange because i checked the value with _controlfp and the global value is changed but not the rounding mode.

UPDATE:

I checked better and what is changed is the value of precision. I think this can really affect the results of rounding.
 
Share this answer
 
v2
Comments
[no name] 6-Jun-17 10:00am    
This should have been added to the question so that it can be responded to appropriately. It seems you have restored the FPU to its default (double) precision. Which function is setting it to single? Aren't you in control of that code as your question suggests?
Optimistic76 8-Jun-17 9:46am    
Unfortunately i don't have control on the component that calls my library. I could only check the value at the beginning of the function call. But i can only guess that maybe an explicit _controlfp in the source code changes the precision of FPU.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900