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

too long float

hello,

why this happened on my python?
>>a=3.9
a
3.8999999999999999

I wanted 3.9 but got 3.89................
How to avoid it? thanks.

this is my python version:
>>sys.version
'2.3.4 (#1, Feb 6 2006, 10:38:46) \n[GCC 3.4.5 20051201 (Red Hat 3.4.5-2)]'
Jan 18 '08 #1
6 1966
On Jan 18, 7:55 am, "J. Peng" <peng....@gmail.comwrote:
hello,

why this happened on my python?
>a=3.9
a

3.8999999999999999

I wanted 3.9 but got 3.89................
How to avoid it? thanks.

this is my python version:
>sys.version

'2.3.4 (#1, Feb 6 2006, 10:38:46) \n[GCC 3.4.5 20051201 (Red Hat 3.4.5-2)]'
>>3.9
3.8999999999999999
>>3.9 == 3.8999999999999999
True

http://effbot.org/pyfaq/why-are-floa...inaccurate.htm
Jan 18 '08 #2
thanks all!

On Jan 18, 2008 3:49 PM, Dennis Lee Bieber <wl*****@ix.netcom.comwrote:
On Fri, 18 Jan 2008 13:55:17 +0800, "J. Peng" <pe******@gmail.com>
declaimed the following in comp.lang.python:

why this happened on my python?
>>a=3.9
>>a
3.8999999999999999

I wanted 3.9 but got 3.89................
How to avoid it? thanks.
Avoid it? You don't... You alleviate the concern by understanding
that floating point is only precise if the value is a fraction of 2: 1,
0.5, 0.25, 0.125...

Computer Science recommends that one does NOT compare two floats for
equality -- instead one should compare the absolute value of the
difference of the two floats against some required epsilon (ie, how far
apart two floats can be and still be considered equal...
abs(f1 - f2) < 0.000001
for example)
--
Wulfraed Dennis Lee Bieber KD6MOG
wl*****@ix.netcom.com wu******@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: we******@bestiaria.com)
HTTP://www.bestiaria.com/

--
http://mail.python.org/mailman/listinfo/python-list
Jan 18 '08 #3
On 2008-01-18, citizen J. Peng testified:
hello,

why this happened on my python?
>>>a=3.9
a
3.8999999999999999
>>a = 3.9
print a
3.9

bart
--
"PLEASE DO *NOT* EDIT or poldek will hate you." - packages.dir (PLD)
http://candajon.azorragarse.info/ http://azorragarse.candajon.info/
Jan 20 '08 #4
In article <sl***********************@localhost.localdomain >,
Bart Ogryczak <z-******@bart.w-wa.plwrote:
>On 2008-01-18, citizen J. Peng testified:
>hello,

why this happened on my python?
>>>>a=3.9
a
3.8999999999999999
>>>a = 3.9
print a
3.9
This has nothing to do with python.

Apparently you don't know the first thing about floating point
numbers. I suggest reading the wikipedia entry.
http://en.wikipedia/wiki/floating_point

Groetjes Albert

>
bart
--
"PLEASE DO *NOT* EDIT or poldek will hate you." - packages.dir (PLD)
http://candajon.azorragarse.info/ http://azorragarse.candajon.info/

--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

Feb 2 '08 #5
Albert van der Horst wrote:
In article <sl***********************@localhost.localdomain >,
Bart Ogryczak <z-******@bart.w-wa.plwrote:
>On 2008-01-18, citizen J. Peng testified:
>>hello,

why this happened on my python?
>a=3.9
>a
3.8999999999999999
a = 3.9
print a
3.9

This has nothing to do with python.

Apparently you don't know the first thing about floating point
numbers. I suggest reading the wikipedia entry.
http://en.wikipedia/wiki/floating_point
Don't think you are the first person to make this mistake, by the way.
Despite Albert's tone suggesting that *everybody* knows about floating
point this is a very common issue among new programmers.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Feb 2 '08 #6
On 2008-02-02, Steve Holden <st***@holdenweb.comwrote:
>Apparently you don't know the first thing about floating point
numbers. I suggest reading the wikipedia entry.
http://en.wikipedia/wiki/floating_point

Don't think you are the first person to make this mistake, by the way.
Despite Albert's tone suggesting that *everybody* knows about floating
point this is a very common issue among new programmers.
And even among old programmers who've not used floating point
much.

--
Grant Edwards grante Yow! I love ROCK 'N ROLL!
at I memorized the all WORDS
visi.com to "WIPE-OUT" in 1965!!
Feb 2 '08 #7

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

Similar topics

2
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in...
2
by: sriamar | last post by:
Hello, How does the type conversion work if the expression involves a float and long int? By K&R 2nd Ed i assume float & long -> float & float . But does the 'long' qualifier affect this...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
3
by: Michael | last post by:
Hallo NG, a few jears ago i made myself in c++ a funktion to convert 4 Byte to one float variable: //########################################################### // Make from 4 Bytes one float...
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,...
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...
2
by: snorble | last post by:
I started creating a simple "bits" class, intended to act like a array of bits. This was my initial idea, basically just overriding the string representation to display the bitmask (so far): ...
3
by: Nader | last post by:
Hello, I have a list of tuple with strin elements. These elements are number, but they are save as string. Now I will change the string to number which will be rounded. An example will make it...
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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.