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

Array and floating point

Hello,

I'm experiencing a problem where the float being appended to the array
is not the same as the result of the appending.
>>from array import *
x = array('f')
x.append(float("0.1"))
x[0]
0.10000000149011612
>>float("0.1")
0.10000000000000001

I'm expecting x[0] = 0.10000000000000001

Thanks
Jonathan Shan

Aug 17 '07 #1
2 1619
Jonathan Shan wrote:
Hello,

I'm experiencing a problem where the float being appended to the array
is not the same as the result of the appending.
>>>from array import *
x = array('f')
x.append(float("0.1"))
x[0]
0.10000000149011612
>>>float("0.1")
0.10000000000000001

I'm expecting x[0] = 0.10000000000000001
array("f") is an array of C floats while Python's float type is a double in
C terms. That's why you lose some precision. Try array("d") instead:
>>from array import array
x = array("d")
x.append(0.1)
x[0]
0.10000000000000001
Peter
Aug 17 '07 #2
Jonathan Shan wrote:
Hello,

I'm experiencing a problem where the float being appended to the array
is not the same as the result of the appending.
>>>from array import *
x = array('f')
x.append(float("0.1"))
x[0]
0.10000000149011612
>>>float("0.1")
0.10000000000000001

I'm expecting x[0] = 0.10000000000000001
'f' denotes a single-precision floating point number. Python's float objects are
double-precision floating point numbers. Use 'd' instead.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Aug 17 '07 #3

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

Similar topics

9
by: Yaroslav Bulatov | last post by:
I made an array of 10 million floats timed how long it takes to sum the elements, here's what I got (millis): gcc -O2: 21 Python with numarray: 104 Python with Numeric: 302...
2
by: Madhusudan Singh | last post by:
I am querying an instrument in a measurement application. The values are returned as IEEE format binary floating point numbers. There are 4 bytes per point. Multiple points are not separated by...
5
by: Koster | last post by:
Just a quickie: I heard that arrays declared in the global scope are automatically initialised with a block of zeros. To help with making my C source compatible with multiple compilers, I'd like...
11
by: gouqizi.lvcha | last post by:
Hi all I have a large vector with float point numbers in it, for example (1.1, 2.1 , 3.2 , 3.3 , 4, 6, 3.2, 8). Is there an easy way to determine how many uique elements in the array? Thanks...
9
by: Haobin | last post by:
Hi everyone, I have an ArrayList whose members are classes. These classes are derived from a same base class. The base class has a floating point array and a string. How do I access each element in...
14
by: rocketman768 | last post by:
Man, that title should be in a poem, but anyways...So, I have this program, and it has an array of 40 million elements. The problem is that I have a for loop that continually is doing stuff to this...
13
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
7
by: Angus | last post by:
I am writing a class which represents a byte array - ie an array of unsigned char. So far I have worked out how to add strings, integers, WORD's, etc. But how would I add a double? Would I...
9
by: ssubbarayan | last post by:
Hi all, I am trying a program to convert floating point values to a byte array and printing the same to the screen.The idea behind this is we already have an existing function which can do byte...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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.