473,396 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

double

Is there an explication why it's executed the then path?

Dim a As Double = 10
Dim b As Double = 3
Dim c As Double = 3
Dim x As Double
Dim y As Double = 10
x = a / b
x *= c
If x = y Then
Debug.Print("x")
End If

Peter
Jul 18 '08 #1
8 1358
Peter Ramsebner wrote:
Is there an explication why it's executed the then path?

Dim a As Double = 10
Dim b As Double = 3
Dim c As Double = 3
Dim x As Double
Dim y As Double = 10
x = a / b
x *= c
If x = y Then
Debug.Print("x")
End If
Your Debug.Print should be outputting x.ToString and y.ToString.

Andrew
Jul 18 '08 #2
Floating point calculations are sometimes wrong. Not always ;-)

Is this because of floating point calculations that you expected different
values for x and y ?

--
Patrice

"Peter Ramsebner" <pe*************@pr.ata écrit dans le message de groupe
de discussion : OW**************@TK2MSFTNGP05.phx.gbl...
Is there an explication why it's executed the then path?

Dim a As Double = 10
Dim b As Double = 3
Dim c As Double = 3
Dim x As Double
Dim y As Double = 10
x = a / b
x *= c
If x = y Then
Debug.Print("x")
End If

Peter

Jul 18 '08 #3
Your Debug.Print should be outputting x.ToString and y.ToString.

Forget the output, the question is how it is possible that x=y?
Jul 18 '08 #4
For now I don't understand what makes you think it shouldn't... x is 10/3*3
so x is 10 and y is 10... This is mathematically correct.

I would assume this is because the intermediate is likely not stored with
full accuracy but basically floating point values are always approched
values. So even a truncated result could give the expected result just by
"chance"...

For example try after the x = a / b line :

Debug.Print(x.ToString("r")) ' This format allows to see the "real" value
stored
Debug.Print(x.ToString)
Debug.Print(3.3333333333333335 * 3) ' Which is 10
Else please be explicit about the problem to avoid wasting our time to
guess...

--
Patrice

"Peter Ramsebner" <pe*************@pr.ata écrit dans le message de groupe
de discussion : ug**************@TK2MSFTNGP02.phx.gbl...
>Your Debug.Print should be outputting x.ToString and y.ToString.

Forget the output, the question is how it is possible that x=y?
Jul 18 '08 #5
Debug.Print(3.3333333333333335 * 3) ' Which is 10
>
it's not 10! It's 10,0000000005
Jul 18 '08 #6
Peter Ramsebner wrote:
> Debug.Print(3.3333333333333335 * 3) ' Which is 10

it's not 10! It's 10,0000000005
= does not mean exact equality for floating point numbers.

It means abs(a-b)<epsilon, where epsilon is some small positive number.

For more words on the subject, see, e.g, section III of
http://www.cprogramming.com/tutorial...sentation.html

Andrew
Jul 18 '08 #7

Don't you see 10 if you run the code ?

The key point to remember is that floating point arithmetic uses *approched*
values because it has to be a compromise between the range and precision
(for example values that are not a power of two have even no exact
representation).

You'll find tons of articles about that on the web but basically my
understanding is :
- floating points values are kept as an approched value
- they are displayed using less significant digits than stored (to increase
the precision and likely not confuse users with visible approximations).
This what the "r" format does. It shows the actual underlying value.

So in most cases those approximations are not visible.

In this particular case :

- 10/3 is stored as a value that multiplied by 3 (and possibly truncated for
user display will give back 10) because the representation of the result of
10/3 is not the same than 9.999 as you have some additional digits resulting
from the calculation that are not shown...

- If you take the visible value and multiple by 3 you won't have 10 but
9.999 etc...

- if you take the actual underlying value and multiply by 3 you'll have back
10 (perhaps 10.00000000005 and possible right digits are either not stored
or truncated for user display giving finally 10)

I'm not sure if this is really something you would like to fix (generally
one complain when the result is not mathematically correct).

If you want to guarantee the precision you could use the Decimal type
instead. If you want to have 9.999999 you'll have to truncate the value to a
fixed size explictely.

As a side note you generally compare floating point values by checking that
the absolute difference is less than a threshold to take into account those
possible approximations...

Hope it helps.

--
Patrice

"Peter Ramsebner" <pe*************@pr.ata écrit dans le message de groupe
de discussion : ue**************@TK2MSFTNGP06.phx.gbl...
> Debug.Print(3.3333333333333335 * 3) ' Which is 10

it's not 10! It's 10,0000000005
Jul 18 '08 #8
Peter Ramsebner wrote:
> Debug.Print(3.3333333333333335 * 3) ' Which is 10

it's not 10! It's 10,0000000005
Which probably 10 is also.

Most numbers can't be represented exactly as a decimal. There is really
no reason why 10 or 3 would be one of them.

--
Göran Andersson
_____
http://www.guffa.com
Jul 18 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Sydex | last post by:
When I compile code I get error C2664: 'Integration::qgaus' : cannot convert parameter 1 from 'double (double)' to 'double (__cdecl *)(double)' in this part : double Integration::quad2d(double...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
10
by: Robert Palma | last post by:
I'm having trouble figuring out how to pass a pointer to a double array (1 dimensional) to a C function. Declaring array as: double xx; Declaring func. int process( double *input ) Calling...
10
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
3
by: BlueTrin | last post by:
I am using a DLL written in C, it uses some pointers on functions, I have defined a wrapper around it in C# which uses some delegates: #region Delegates and Marshalling to call solvopt public...
67
by: lcw1964 | last post by:
This may be in the category of bush-league rudimentary, but I am quite perplexed on this and diligent Googling has not provided me with a clear straight answer--perhaps I don't know how to ask the...
1
by: JWest46088 | last post by:
I keep getting these error messages: area(double,double) in Rectangle cannot be applied to () return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter(); ...
2
by: dj10fld | last post by:
I am getting a (cannot convert double to double in assignment errors) here is a part of my code #include <iostream> #include <iomanip> #include <cmath> using namespace std; #define MaxSize...
2
by: Genro | last post by:
#include<stdio.h> #include<TX/graphics.h> #include<time.h> // I need help! struct Krug{ double _x; double _y; double _skox; double _skoy; double...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.