473,503 Members | 12,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

float and double question

Pat
Give two double-typed variable X and Y.

If (X==Y) is true,

then how about the following results:
(float(X) > float(Y))?
(float(X) < float(Y))?
(float(X) >= float(Y))?
( X > float(Y) )?
( X < float(Y) )?
( X == float(Y) )?

Will the results be independent of compliers and OS platforms?

Thanks. Pat
Jul 22 '05 #1
5 1860
"Pat" <Pa*@Pat.com> wrote...
Give two double-typed variable X and Y.

If (X==Y) is true,

then how about the following results:
(float(X) > float(Y))?
(float(X) < float(Y))?
(float(X) >= float(Y))?
( X > float(Y) )?
( X < float(Y) )?
( X == float(Y) )?

Will the results be independent of compliers and OS platforms?


Unless there is an overflow (resulting in infinity values), the
expression

float(X) == float(Y)

should always evaluate 'true' if X == Y evaluates 'true'.

Also, if you attempt to compare float(X) and Y [or X and float(Y)],
both will be converted back to 'double' and the result will most
definitely depend on values of X and Y, compiler, and hardware.

Victor
Jul 22 '05 #2
Pat,

I did some experiments, I found that float(X) is rounding on my MVC++
compiler, so if X=0.499999999, float(X) = 0.5, but if X = 0.511111111,
float(X) = 0.51111, so we get both > and < situations.

If X == Y, then always float(X) == float(Y)
dave
"Pat" <Pa*@Pat.com> wrote in message news:40**********@rain.i-cable.com...
Give two double-typed variable X and Y.

If (X==Y) is true,

then how about the following results:
(float(X) > float(Y))?
(float(X) < float(Y))?
(float(X) >= float(Y))?
( X > float(Y) )?
( X < float(Y) )?
( X == float(Y) )?

Will the results be independent of compliers and OS platforms?

Thanks. Pat

Jul 22 '05 #3
Ian
Dave Townsend wrote:
Pat,

I did some experiments, I found that float(X) is rounding on my MVC++
compiler, so if X=0.499999999, float(X) = 0.5, but if X = 0.511111111,
float(X) = 0.51111, so we get both > and < situations.
Think about this for a moment; abs(0.5-499999999) !=
abs(0.5-0.511111111). 7 orders of magnitude difference.

Ian
If X == Y, then always float(X) == float(Y)
dave
"Pat" <Pa*@Pat.com> wrote in message news:40**********@rain.i-cable.com...
Give two double-typed variable X and Y.

If (X==Y) is true,

then how about the following results:
(float(X) > float(Y))?
(float(X) < float(Y))?
(float(X) >= float(Y))?
( X > float(Y) )?
( X < float(Y) )?
( X == float(Y) )?

Will the results be independent of compliers and OS platforms?

Thanks. Pat


Jul 22 '05 #4

Ian,

Sorry, not following what you said.

I'm saying that because the float type casting operation rounds, in some
cases
it will round down and some it will round up, so we get both < and > for the
examples.

dave

"Ian" <no***@nowhere.com> wrote in message
news:10***************@drone5.qsi.net.nz...
Dave Townsend wrote:
Pat,

I did some experiments, I found that float(X) is rounding on my MVC++
compiler, so if X=0.499999999, float(X) = 0.5, but if X = 0.511111111,
float(X) = 0.51111, so we get both > and < situations.

Think about this for a moment; abs(0.5-499999999) !=
abs(0.5-0.511111111). 7 orders of magnitude difference.

Ian
If X == Y, then always float(X) == float(Y)
dave
"Pat" <Pa*@Pat.com> wrote in message news:40**********@rain.i-cable.com...
Give two double-typed variable X and Y.

If (X==Y) is true,

then how about the following results:
(float(X) > float(Y))?
(float(X) < float(Y))?
(float(X) >= float(Y))?
( X > float(Y) )?
( X < float(Y) )?
( X == float(Y) )?

Will the results be independent of compliers and OS platforms?

Thanks. Pat


Jul 22 '05 #5
Pat wrote:
Give two double-typed variable X and Y.

If (X==Y) is true,

then how about the following results:
(float(X) > float(Y))?
(float(X) < float(Y))?
(float(X) >= float(Y))?
( X > float(Y) )?
( X < float(Y) )?
( X == float(Y) )?

Will the results be independent of compliers and OS platforms?

Thanks. Pat


No, they will not be independent of compilers, nor of OS platforms.
Floating point numbers, the underlying machine instructions and
registers that manage them are different. You will get subtle
differences, especially as you increase in precision. We have trouble on
a regular basis with floating point numbers in our software, compounded
when we port to different platforms.

Floating point numbers are good to use in some situations, but you
REALLY want to avoid comparing them whenever possible.

David Logan
Jul 22 '05 #6

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

Similar topics

11
102438
by: Christian Christmann | last post by:
Hi, just a general question. Which data type is more precise for large values: float or double? Thank you. Chris
54
8291
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This...
5
66175
by: guy | last post by:
Can someone point me to page that gives a decision tree on deciding when to use float or double in a C# application including performance impacts of using one over the other. And when is decimal...
1
1613
by: Bruno van Dooren | last post by:
Hi, i have a simple question that has bothered me for some time now. if i have a function foo( double a) and i do something like float b = 2.0; foo(b);
8
5436
by: abdul_n_khan | last post by:
Hello, I have a basic question related to datatypes. I am trying to read a value using Microsoft's ADO recordset from a field (lets call it 'Price') with datatype decimal(19,6) => 19 = Precision,...
60
7158
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
3
1745
by: hantechs | last post by:
<html> <body> <p style="width:30%;">text1</p> <p style="float:left;">text2</p> </body> </html> The effect of this html code is : text1 and text2 each is on a line. My question is: Why text2...
116
35671
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
13
6153
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
22
2742
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
7098
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
7364
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...
1
7017
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
7470
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
5604
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,...
1
5026
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...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
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...
1
751
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.