Hi,
i have strings which look like money values (ie 34.45)
is there a way to convert them into float variables?
everytime i try I get this error: "numb = float(my_line) ValueError:
empty string for float()"
"
here's the code
************
import sys
import re
for line in sys.stdin.readl ines():
my_line = line.rstrip()
numb = float(my_line)
print numb 16 4134
Ben Finney wrote:
You most likely do *not* want floating-point numbers for currency,
since they rely on the operating system's binary floating point
support which cannot accurately represent decimal fractions.
I've heard (ok, read) that several times now and I understand the
argument. But what use is there for floats, then? When is it OK to use them?
/W
Wildemar Wildenburger wrote:
Ben Finney wrote:
>You most likely do *not* want floating-point numbers for currency, since they rely on the operating system's binary floating point support which cannot accurately represent decimal fractions.
I've heard (ok, read) that several times now and I understand the
argument. But what use is there for floats, then? When is it OK to use them?
There are many, many situations where one is *not* trying to represent numbers
that have nice decimal fractions or the error induced is insignificant to the
problem. For example, I might take temperature readings good to 0.1 degrees
Celcius. I'll type those numbers in decimal even though when I do my
calculations I'll use floating point math because the error (about 1e-15 or so)
is so far below the error in my measurement (about 0.1) that I won't have problems.
Binary floating point has the advantage of being widely implemented and quite
fast compared to decimal floating point.
--
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
Wildemar Wildenburger <la*********@kl apptsowieso.net writes:
But what use is there for floats, then? When is it OK to use them?
When one is willing to sacrifice decimal precision for speed of
calculation, and doesn't need the numbers to stay precise. E.g. when
performing millions of calculations on real-world (analogue)
measurements.
--
\ "Too many Indians spoil the golden egg." -- Sir Joh |
`\ Bjelke-Petersen |
_o__) |
Ben Finney
Ben Finney wrote:
Wildemar Wildenburger <la*********@kl apptsowieso.net writes:
>But what use is there for floats, then? When is it OK to use them?
When one is willing to sacrifice decimal precision for speed of
calculation, and doesn't need the numbers to stay precise. E.g. when
performing millions of calculations on real-world (analogue)
measurements.
Traditionally, mainframe computers used BCD arithmetic to handle
currency, and the Decimal module is probably the best way to proceed if
your Python is recent enough to include it.
That isn't to say that currencies can't be handled using floating-point.
The important thing then becomes to round after each calculation rather
than allowing the errors to build up until they become significant
enough to make a difference to the final result.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
On 31 Aug, 02:12, Wildemar Wildenburger
<lasses_w...@kl apptsowieso.net wrote:
I've heard (ok, read) that several times now and I understand the
argument. But what use is there for floats, then? When is it OK to use them?
There are fractions that can be exactly represented by floats that
cannot be exactly represented by decimals. There are fractions that
can be exactly represented by decimals that cannot be exactly
represented by floats.
Which one is better? Which do we prefer?
What a float cannot do is to represent a decimal fractional number
(e.g. 1.1) exactly. If we need that, we cannot use floats. A notable
example is monetary computations, it covers 99% of the use for decimal
numbers in computers. For this reason, we should never use floats to
add 10 cents to a dollar. The use of decimals for monetary
calculations is mandatory.
Floats are written as decimal fractional numbers in program code, and
they are printed as decimal fractional numbers on the screen. A novice
programmer may for this reason easily mistake floats for being
decimals. But inside the computer floats are something very different.
Floats do not represent decimal fractional numbers. Floats represent
binary fractional numbers. Decimal numbers are base-10, binary numbers
are base-2. Floats and decimals are similar but different.
It is much easier to manipulate for computers than decimals. Digital
electronics by convention only has two states, TTL voltage levels 0.0
V and 5.0 V, which are taken to mean 0 and 1 in binary (or false and
true in boolean) respectively. Computers work internally with binary
numbers. Floats are preferred to decimals in e.g. numerical maths and
other scientific computing for their better performance speed wise.
Knowledge of the behaviour of floats, e.g. how they cause rounding and
truncation errors, is pivotal in that field of computer science. For
most purposes, we could replace floats with decimals. But then we
would suffer a major speed penalty.
Decimals are only "precise" if the input values can be represented
precisely by decimal fractions. If we typed 1.1 and took that to mean
"one dollar and 10 cents", then certainly "exactly 1.1" was what we
meant. But if we ment "1.1 centigrades read from a mercury
thermometer", we would actually (by convention) mean "something
between 1.05 and 1.14 centigrades". Decimals also fail to be exact if
we try to compute things like "1.0/3.0". 1/3 does not have an exact
representation in decimal, and we get an approximation from the
computer.
For purposes where exact precision is not needed, decimals are neither
better nor worse than floats. This accounts for 99% of the cases where
fractional numbers are used on a computer.
More about floating point numbers here: http://www.math.byu.edu/~schow/work/...atingPoint.htm
Sturla Molden
sturlamolden wrote:
On 31 Aug, 02:12, Wildemar Wildenburger
<lasses_w...@kl apptsowieso.net wrote:
>I've heard (ok, read) that several times now and I understand the argument. But what use is there for floats, then? When is it OK to use them?
There are fractions that can be exactly represented by floats that
cannot be exactly represented by decimals.
Would you care to give an example?
There are fractions that
can be exactly represented by decimals that cannot be exactly
represented by floats.
Which one is better? Which do we prefer?
What a float cannot do is to represent a decimal fractional number
(e.g. 1.1) exactly. If we need that, we cannot use floats. A notable
example is monetary computations, it covers 99% of the use for decimal
numbers in computers. For this reason, we should never use floats to
add 10 cents to a dollar. The use of decimals for monetary
calculations is mandatory.
That last sentence is patent nonsense, and completely untrue. Many
satisfactory financial applications have been written using only
floating-point arithmetic. Indeed I believe the accountant's Swiss army
knife, the Excel spreadsheet, uses floating-point numbers exclusively.
What you say about floating-point have speed advantages is true, but you
go too far in claiming that decimal arithmetic is mandatory for monetary
calculations. That's about as sensible as saying that base 12 and base
20 arithmetic units were required to calculate in pounds, shillings and
pence.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
On 8/31/07, Steve Holden <st***@holdenwe b.comwrote:
sturlamolden wrote:
On 31 Aug, 02:12, Wildemar Wildenburger
<lasses_w...@kl apptsowieso.net wrote:
I've heard (ok, read) that several times now and I understand the
argument. But what use is there for floats, then? When is it OK to use them?
There are fractions that can be exactly represented by floats that
cannot be exactly represented by decimals.
Would you care to give an example?
There are fractions that
can be exactly represented by decimals that cannot be exactly
represented by floats.
Which one is better? Which do we prefer?
What a float cannot do is to represent a decimal fractional number
(e.g. 1.1) exactly. If we need that, we cannot use floats. A notable
example is monetary computations, it covers 99% of the use for decimal
numbers in computers. For this reason, we should never use floats to
add 10 cents to a dollar. The use of decimals for monetary
calculations is mandatory.
That last sentence is patent nonsense, and completely untrue. Many
satisfactory financial applications have been written using only
floating-point arithmetic. Indeed I believe the accountant's Swiss army
knife, the Excel spreadsheet, uses floating-point numbers exclusively.
This is true, although Excel munges it's FP to provide "expected" results.
It depends on what you consider a "financial" application though.
Excel, while in extremely broad use, is not used to implement any of
the systems which actually "define" money, like the back end financial
systems at banks and credit unions.
In my experience, by far the most common method of calculating
financial numbers is actually using integer amounts, and then applying
well-defined rounding rules which I can't be bothered to look up the
name for.
For what it's worth, the work that I do with money (which is
middleware doing data transport, not calculations or billing) uses
either string representations or fixed point.
What you say about floating-point have speed advantages is true, but you
go too far in claiming that decimal arithmetic is mandatory for monetary
calculations. That's about as sensible as saying that base 12 and base
20 arithmetic units were required to calculate in pounds, shillings and
pence.
I believe that to the degree that "real" accounting was done in those
currencies it did in fact use non-decimal bases. Just as people don't
use decimal time values (except us crazy computer folk), you're write
1 pound 4 shillings, not 1.333... pounds.
On Aug 31, 5:28 pm, "Chris Mellon" <arka...@gmail. comwrote:
On 8/31/07, Steve Holden <st...@holdenwe b.comwrote:
sturlamolden wrote:
On 31 Aug, 02:12, Wildemar Wildenburger
<lasses_w...@kl apptsowieso.net wrote:
>I've heard (ok, read) that several times now and I understand the
>argument. But what use is there for floats, then? When is it OK to use them?
There are fractions that can be exactly represented by floats that
cannot be exactly represented by decimals.
Would you care to give an example?
There are fractions that
can be exactly represented by decimals that cannot be exactly
represented by floats.
Which one is better? Which do we prefer?
What a float cannot do is to represent a decimal fractional number
(e.g. 1.1) exactly. If we need that, we cannot use floats. A notable
example is monetary computations, it covers 99% of the use for decimal
numbers in computers. For this reason, we should never use floats to
add 10 cents to a dollar. The use of decimals for monetary
calculations is mandatory.
That last sentence is patent nonsense, and completely untrue. Many
satisfactory financial applications have been written using only
floating-point arithmetic. Indeed I believe the accountant's Swiss army
knife, the Excel spreadsheet, uses floating-point numbers exclusively.
This is true, although Excel munges it's FP to provide "expected" results.
It depends on what you consider a "financial" application though.
Excel, while in extremely broad use, is not used to implement any of
the systems which actually "define" money, like the back end financial
systems at banks and credit unions.
In my experience, by far the most common method of calculating
financial numbers is actually using integer amounts, and then applying
well-defined rounding rules which I can't be bothered to look up the
name for.
For what it's worth, the work that I do with money (which is
middleware doing data transport, not calculations or billing) uses
either string representations or fixed point.
What you say about floating-point have speed advantages is true, but you
go too far in claiming that decimal arithmetic is mandatory for monetary
calculations. That's about as sensible as saying that base 12 and base
20 arithmetic units were required to calculate in pounds, shillings and
pence.
I believe that to the degree that "real" accounting was done in those
currencies it did in fact use non-decimal bases. Just as people don't
use decimal time values (except us crazy computer folk), you're write
1 pound 4 shillings, not 1.333... pounds.
FYI, 1 pound 4 shillings was 1.20 pounds; 1.333... pounds was 1 pound
6 shillings 8 pence. I think...
In article <ma************ *************** ***********@pyt hon.org>,
Chris Mellon <ar*****@gmail. comwrote:
I believe that to the degree that "real" accounting was done in those
currencies it did in fact use non-decimal bases. Just as people don't
use decimal time values (except us crazy computer folk), you're write
1 pound 4 shillings, not 1.333... pounds.
When I worked on the British Railways National Payroll system, about 35
years ago, we, in common with many large users, wrote our system to deal
with integer amounts of pennies, and converted to pounds, shillings and
pence in the output part of the system.
--
David Wild using RISC OS on broadband www.davidhwild.me.uk This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jørgen Cederberg |
last post by:
Hi,
using Python 2.2.1 on Windows and QNX I'm having trouble understandig
why int() raises a ValueError exception upon converting strings.
>>> int('-10')
-10
>>> int('10')
10
>>> int(10.1)
|
by: dataangel |
last post by:
I wrote a function to compare whether two strings are "similar" because
I'm using python to make a small text adventure engine and I want to it
to be responsive to slight mispellings, like "inevtory" and the like. To
save time the first thing my function does is check if I'm comparing an
empty vs. a non-empty string, because they are to be never considered
similar. Right now I have to write out the check like this:
if str1 and str2:
if...
|
by: BGP |
last post by:
I am working on a WIN32 API app using devc++4992 that will accept Dow
Jones/NASDAQ/etc. stock prices as input, parse them, and do things with
it. The user can just cut and paste back prices into a window and hit
a button to process it.
The information thus enters the program as a char array. Prices can be
between $1 and $100, including cents. So we can have prices such as
3.01, 1.56, 11.57, etc. The char array is an alphanumeric...
|
by: Madhusudan Singh |
last post by:
Is it possible to convert a very long list of strings to a list of floats in
a single statement ?
I have tried float(x) and float(x) but neither work. I guess I would have
to write a loop if there isn't a way.
|
by: MeganTSU |
last post by:
Hey yall! I am trying to get this program finished for class.... It
says that you are suppposed to write a program that will display a
check formatted out put (the output looks like a check). I got
everything to work and it will compile and run......
BUT.... I dont know how to turn the 'Amount' into a charater string
that puts the number into words.... It is supposed to show the name,
then the amount, and then on the next line write out...
| |
by: bdog4 |
last post by:
I want to validate a number to so that they can only input $2.00, 5.00,
7.00, 50.00 etc... I don't wan them to be able to put cents on the
amount. It can either either be 2 or 2.00 Any ideas on how I can do
this? Thanks
|
by: Chad |
last post by:
I would like to remove the decimal but still show the cents using
sprintf.
char empheader;
double totalwage = 123.45;
sprintf(empheader, "%.2f", totalwage);
|
by: SergioQ |
last post by:
Hi all, am trying to show a dollar figure on my webpage, but defining
this for the cents value:
font-size: 80%;
vertical-align: super;
just isn't cutting it!
Is there a way to reduce the spacing before the decimal value, so I
canbring it closer to the dollar value?
|
by: canabatz |
last post by:
i need help creating a from for input price
i want two separate fields to hold dollars and cents ,and when i press on submit
the all price will be sent as: lets say 12.45
how can i do it ,im new to JS worls :)
thanx in advance!!!
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |