473,387 Members | 1,542 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,387 software developers and data experts.

float bug or bad implementation

Hi!

Let's see, we got:

float var=4.6f; //as we know 414/4.6 shoud be equal to 90

but Math.Ceiling(414/var) gives us 91

but (414/var).ToString() prints '90'.

And when we convert:
((double)(414/var)).ToString() we got '90,0000018658846'

I know how the floats are represented in memory and I know
limitations connected with, but why compiler and program
makes bugs in such simple cases??

Adam
Nov 15 '05 #1
5 2313
I'm not too surprised that Math.Ceiling(414/var) gives 91.

Floating point representation is such that there is no floating point number
that matches 4.6f, only numbers that approximate it. Then,
Math.Ceiling(414/var) may very well return 91. This is not a bug, just a
consequence of floating point representation and computation rules.

So, before classifying these odd behaviors as "bugs", I suggest that you
read the specs (IEEE FP specs, C# specs) and see if the results that you get
contradict the specs. I doubt that that the odd results that you experienced
are actually bugs.

If you want to get predictable rounding, you should use the decimal type.
Double will be a bit better than float, but still, you will get odd
behaviors, this is inherent to the FP representation.

Bruno.

"Kubik" <ku***@iname.com> a écrit dans le message de
news:O3**************@TK2MSFTNGP11.phx.gbl...
Hi!

Let's see, we got:

float var=4.6f; //as we know 414/4.6 shoud be equal to 90

but Math.Ceiling(414/var) gives us 91

but (414/var).ToString() prints '90'.

And when we convert:
((double)(414/var)).ToString() we got '90,0000018658846'

I know how the floats are represented in memory and I know
limitations connected with, but why compiler and program
makes bugs in such simple cases??

Adam

Nov 15 '05 #2

Użytkownik "Bruno Jouhier [MVP]" <bj******@club-internet.fr> napisał w
wiadomo¶ci news:ef**************@TK2MSFTNGP10.phx.gbl...
Floating point representation is such that there is no floating point number that matches 4.6f, only numbers that approximate it. Then,
Math.Ceiling(414/var) may very well return 91. This is not a bug, just a
consequence of floating point representation and computation rules.

Yes, I agree, but it's very irritating. Especially when ToString(x+"="+y)
method
returns '50=50' string, but condition x==y isn't true. I was using for all
time
PHP interpreter, and there wasn't such problems with so simple values.
Moreover in C/C++ I also didn't experience such problems.

OK, finally I used decimal type. Thanks for your answer.

best regards
Adam
Nov 15 '05 #3

"Kubik" <ku***@iname.com> wrote in message
news:O3**************@TK2MSFTNGP11.phx.gbl...
Hi!

Let's see, we got:

float var=4.6f; //as we know 414/4.6 shoud be equal to 90

but Math.Ceiling(414/var) gives us 91

but (414/var).ToString() prints '90'.

And when we convert:
((double)(414/var)).ToString() we got '90,0000018658846'

I know how the floats are represented in memory and I know
limitations connected with, but why compiler and program
makes bugs in such simple cases??

Adam


Because the values are stored in binary. The value "4.6" is exact when
represented in base 10, but in base 2 it becomes an infinitely repeating
fraction "100.1001 1001 1001 ..." and therefore can only be approximated.

If you want to use base-10 for the internal representation, you can use the
"decimal" type which is C# equivalent to System.Decimal. This type uses base
10 for storing its exponent instead of base 2 used by other floating-point
types. "4.6" is stored as 46 x 10^-1 in the System.Decimal type.
Nov 15 '05 #4

"Kubik" <ku***@iname.com> wrote in message
news:O3**************@TK2MSFTNGP11.phx.gbl...
Hi!

Let's see, we got:

float var=4.6f; //as we know 414/4.6 shoud be equal to 90

but Math.Ceiling(414/var) gives us 91

but (414/var).ToString() prints '90'.

And when we convert:
((double)(414/var)).ToString() we got '90,0000018658846'

I know how the floats are represented in memory and I know
limitations connected with, but why compiler and program
makes bugs in such simple cases??


floats, doubles and integers are finite subsets of the rational numbers.
floats are basically a subset of doubles, and doubles and integers are
mostly disjoint.

An integer can be "converted" to a float, but it's often just mapped to a
nearby float. The
A float and an integer are said to be equal if they are equal after the
integer is mapped to a float.

So

float f = 414/4.6f; // f == (float)90 since f is the float nearest to 90
double d1 = (double)(414/4.6d); //f == (double)90 since d1 is the double
nearest to 90
double d2 = (double)(414/4.6f); //f != (double)90

d2 != (double)90 because d2 was converted from a float (414.4/6f), and so it
is only as close to 90 as a float can get. A double can get a whole lot
closer.

Graphically it looks like this:
-
-90.000002 <----- the nearest float to 90 ( == f and d2)
-
-
-
-
-
-
-90.00000000000002 <---- the nearest double to 90 (d1)
-90
-
David
Nov 15 '05 #5
This article should help:

http://portal.acm.org/citation.cfm?i...TOKEN=43458177

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Kubik" <ku***@iname.com> wrote in message
news:O3**************@TK2MSFTNGP11.phx.gbl...
Hi!

Let's see, we got:

float var=4.6f; //as we know 414/4.6 shoud be equal to 90

but Math.Ceiling(414/var) gives us 91

but (414/var).ToString() prints '90'.

And when we convert:
((double)(414/var)).ToString() we got '90,0000018658846'

I know how the floats are represented in memory and I know
limitations connected with, but why compiler and program
makes bugs in such simple cases??

Adam

Nov 15 '05 #6

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

Similar topics

10
by: Alexander Malkis | last post by:
What is the biggest float number f so that -f doesn't overflow and is still representable as a float. -- Best regards, Alex. PS. To email me, remove "loeschedies" from the email address...
26
by: 69dbb24b2db3daad932c457cccfd6 | last post by:
Hello, I have to initialize all elements of a very big float point array to zero. It seems memset(a, 0, len) is faster than a simple loop. I just want to know whether it is safe to do so, since I...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
8
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,...
8
by: bearophileHUGS | last post by:
sys.maxint gives the largest positive integer supported by Python's regular integer type. But maybe such attribute, with few others (they can be called min and max) can be given to int type itself....
60
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
10
by: Chris Stankevitz | last post by:
Is this a fast way to invert a float: inline Invert(float& f) { f *= -1.0f; } I'd like the CPU to flip the sign bit (and not carry out a float-float multiplication). Please enlighten me! ...
14
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a stream?
3
by: =?GB2312?B?uN/B+g==?= | last post by:
#include <stdio.h> #include <stdlib.h> int main(void) { float j = 1.0f, i =j; int n = 0; for(; i - j - 1.00f; ++n, j /= 10) { ;
8
by: d major | last post by:
I was very puzzled about the conversion between float and long, I cann't understand why a long val can convert to a float, as the below codes show: typedef unsigned long u_long; float val =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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,...

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.