473,401 Members | 2,127 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,401 software developers and data experts.

Error in "double = double - double" statement

I am trying to compute the differnece between two dwords in visual basic and
keep getting the wrong value being computed. I have the following structure
defind:

Public Type HighLowQuote
TradeDate As String
OpenPrice As Double
HighPrice As Double
LowPrice As Double
ClosePrice As Double
Volume As Long
AdjClosePrice As Double
AmtChange As Double
End Type

I create an instance of this type as 'q'. I then do the following:
q.highprice = variant
q.lowprice = variant

at this point the locals variable window shows the values as:
q.highprice = 4.1
q.lowprice = 4

I then fall through the following code:
q.AmtChange = q.highprice - q.lowprice

q.amtchange is then valued at 9.99999999999996E-02 in the locals window when
I believe it should be valued at .1.

Why is the value not being set to .1? Is there a change I can make to get
this calculation to work how I expect or is there some other way to perform
these calculations to get the results I am after?

Any help would be appreciated. Thanks in advance.
Jul 17 '05 #1
3 3226
"Mensan" <do*********@hotmail.com> wrote
I am trying to compute the differnece between two dwords in visual basic and
keep getting the wrong value being computed. I have the following structure
defind:

Public Type HighLowQuote
TradeDate As String
OpenPrice As Double
HighPrice As Double
LowPrice As Double
ClosePrice As Double
Volume As Long
AdjClosePrice As Double
AmtChange As Double
End Type

Have you tried declaring your Price values as Currency instead of Double?

LFS

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 17 '05 #2

"Mensan" <do*********@hotmail.com> wrote in message
news:py****************@fe2.texas.rr.com...
I am trying to compute the differnece between two dwords in visual basic and keep getting the wrong value being computed. I have the following structure defind:

Public Type HighLowQuote
TradeDate As String
OpenPrice As Double
HighPrice As Double
LowPrice As Double
ClosePrice As Double
Volume As Long
AdjClosePrice As Double
AmtChange As Double
End Type

I create an instance of this type as 'q'. I then do the following:
q.highprice = variant
q.lowprice = variant

at this point the locals variable window shows the values as:
q.highprice = 4.1
q.lowprice = 4

I then fall through the following code:
q.AmtChange = q.highprice - q.lowprice

q.amtchange is then valued at 9.99999999999996E-02 in the locals window when I believe it should be valued at .1.

Why is the value not being set to .1? Is there a change I can make to get this calculation to work how I expect or is there some other way to perform these calculations to get the results I am after?

Any help would be appreciated. Thanks in advance.


As Larry said, you may want to use the Currency type instead of doubles.

Welcome to the world of floating point numbers. In your debugger, try
examining the value of
?q.highprice
and then
?(q.highprice = 4.1)
and then
?q.highprice - 4

Depending on where the 4.1 came from, you may discover that it isn't
actually 4.1 in the first place. I can't really tell, because all you
said was q.highprice = variant.
Jul 17 '05 #3
Mensan <do*********@hotmail.com> schreef in berichtnieuws
py****************@fe2.texas.rr.com...

Hello Mensan,

[Snip]
q.amtchange is then valued at 9.99999999999996E-02 in the locals window when I believe it should be valued at .1.

Why is the value not being set to .1? Is there a change I can make to get
this calculation to work how I expect or is there some other way to perform these calculations to get the results I am after?


But they are allmost the same ! They just differ a meager
0.000000000000004. That's not even a promille of a promille !

Even if you would get that ammount in dollars *every second*, you would,
after a year, not even have a cent. That would actually take something in
the vincinity of a mere 100,000 years :-)

9.99999999999996E-02. The E-02 means : shift the decimal-point left two
places to get the actual number. This means that the actual number is
0.0999999999999996 . And I think that's mighty close to 0.1 .
As others allready explained, that difference is a problem you can get when
a system that can only store numbers in powers of 2 tries to store a
fractional number that is based on powers of 10.

Just try to create the number 0.3 if you can only use 1/2 , 1/4 , 1/8 , 1/16
, etc (wich are 2^-1 , 2^-2, 2^-3, 2^-4, etc) which are the numbers the
computer has to use. I think you will see that you can't ...
To get rid of the *effect*, you could use a storage-type that automatically
rounds on the number of decimals you need (as suggested, use the
currency-type)

On computers that have not got that kind of variable-types you either ignore
the rounding-effect by displaying it with a { using "#####.##" } method
(which will do the rounding for you), or by not using decimals (calculate
the numbers in whole numbers (cents) only. Which, by the way, is what the
currency-type in VB might do).
By the way : This (your problem) is the reason why you should *never* try to
compare two doubles (or even two numbers having fractional parts) with each
other for equality (Like : if double1 = double2 then ....).

Regards,
Rudy Wieser

Jul 17 '05 #4

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

Similar topics

5
by: Piotr B. | last post by:
Hello, I use MingGW g++ 3.2.3 on Windows 2000/AMD Athlon XP. I tried to output a "long double" variable using stdio printf(). I've tried various %formats (%llf, %Lf etc.), but none of them...
2
by: Sooha Park Lee | last post by:
I would like to compare two "double" numbers. To overcome round-off error, I am using the following strategy: rounding each number and try to convert it into interger by multiplying 10E6. One...
22
by: bq | last post by:
Hello, Two questions related to floating point support: What C compilers for the wintel (MS Windows + x86) platform are C99 compliant as far as <math.h> and <tgmath.h> are concerned? What...
4
by: michi | last post by:
Hello How can I convert a string to double with a decimal point as seperator? ***CODE**** strNumber ="12.00" dblDouble = Cdbl(strNumber) ****
0
by: Dean N. Williams | last post by:
Dear Python and Mac Community, I have just successfully built gcc version 4.1.0 for my Mac OS X 10.4.6. gcc -v Using built-in specs. Target: powerpc-apple-darwin8.6.0 Configured with:...
12
by: Zero | last post by:
Hi everybody, i want to write a small program, which shows me the biggest and smallest number in dependance of the data type. For int the command could be: ...
2
by: anon.asdf | last post by:
Hi! Q. 1) How does one write: sizeof(array of 5 "pointers to double") ??? I know that sizeof(pointer to an array of 5 doubles) can be written as: sizeof(double (*));
6
by: bananaguyc | last post by:
Okay, I'm running the following code in GCC 4.1.2: <stdio.h> int main(void) { long double test = 4.67e-4; printf("float: %f e-notation: %e\n", test, test); return 0; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.