473,796 Members | 2,704 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1370
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.T oString("r")) ' This format allows to see the "real" value
stored
Debug.Print(x.T oString)
Debug.Print(3.3 333333333333335 * 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.3 333333333333335 * 3) ' Which is 10
>
it's not 10! It's 10,0000000005
Jul 18 '08 #6
Peter Ramsebner wrote:
> Debug.Print(3.3 333333333333335 * 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.3 333333333333335 * 3) ' Which is 10

it's not 10! It's 10,0000000005
Jul 18 '08 #8
Peter Ramsebner wrote:
> Debug.Print(3.3 333333333333335 * 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
9895
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 (*func)(double,double)) { nfunc = func ; return qgaus(f1,x1,x2);//error there
20
17842
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; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
31
6652
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 variable becomes 364 and the other one becomes 365. Does anyone have any insight to what the problem is? Thanks in advance. Bjørn
10
8662
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 func. as one of the following:
10
18775
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
2886
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 delegate double funCallback(double x); public delegate double funcCallback(double x); public delegate void gradCallback(double x, double v); public delegate void gradcCallback(double x, double v);
67
9932
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 quesion. I have begun to familiarize myself here with the gcc compiler in a win32 environment, in the form of MinGW using both Dev C++ and MSYS as interfaces. I have recompiled some old math code that uses long double types throughout and...
1
8229
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(); ^ perimeter(double,double) in Rectangle cannot be applied to () return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter(); ^ setSides(double,double) in Rectangle cannot be applied to (double)...
2
5598
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 1000 double loancalc (double *months, double *intrat, double *princ, double calcprnc, double calcpay, double calcbal, double calcnew, double calcInt); double display (double *months, double calcprnc, double calcpay, double calcbal, double...
2
5808
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 _granx1;
0
9535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10467
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10201
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4130
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.