Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to truncate/round-off decimal numbers?

Reply
Thread Tools

How to truncate/round-off decimal numbers?

 
 
jean-michel bain-cornu
Guest
Posts: n/a
 
      06-21-2006
> just a thought: if you *always* work with "floats" with two decimals,
> you are in fact working with integers, but you represent them as a
> floats - confusing for the internal representation.
>
> So why not work with int(float * 100) instead? This way you only have
> to take care of roundoffs etc when dividing.

And why won't you work with decimal module ?
 
Reply With Quote
 
 
 
 
Nick Maclaren
Guest
Posts: n/a
 
      06-21-2006

In article <(E-Mail Removed) .com>,
"per9000" <(E-Mail Removed)> writes:
|>
|> just a thought: if you *always* work with "floats" with two decimals,
|> you are in fact working with integers, but you represent them as a
|> floats - confusing for the internal representation.

No, you aren't - you are working with fixed-point, which is something
that is neither integers nor floating-point, but is somewhere in
between. I am (just) old enough to remember when it was used for
numeric work, and to have used it for that myself, but not old enough
to have done any numeric work using fixed-point hardware.

|> So why not work with int(float * 100) instead? This way you only have
|> to take care of roundoffs etc when dividing.

And multiplying, and calling most mathematical functions.


Regards,
Nick Maclaren.
 
Reply With Quote
 
 
 
 
per9000
Guest
Posts: n/a
 
      06-21-2006
Nick Maclaren wrote:
> |> just a thought: if you *always* work with "floats" with two decimals,
> |> you are in fact working with integers, but you represent them as a
> |> floats - confusing for the internal representation.
>
> No, you aren't - you are working with fixed-point


Nick, your answer has so many layers, I'll try to explain how I think


1) if you use integers you can think of them as having one part bigger
than 100 and one part smaller than 100, like so:
>>> a = 11122
>>> (a/100,a%100)

(111, 22)
Here the output 111,22 looks like something else than an integer, but
this is just a matter of representation. a *is* an integer, but we
represent it *as if it was* a "decimal" number. (Compare with
(minutes,seconds) or (euro,cents) or (feet,inch) or any other
"arbitrary" position system)

2) If we use floats with two decimals
>>> b = 222.33
>>> b

222.33000000000001
they look like fix-point numbers (I had to look it up
http://en.wikipedia.org/wiki/Fixed-point ) but python stores it
(correct me if I am wrong) as a float (or double or quad or whatever).
If we want to work with fix-point aritmetics we have to invent new
functions to do most math.

3) Most "decimal numbers" cannot be stored exactly as floats - that is
why b gave the ugly print. But some can, f.x
>>> quart = 0.25
>>> quart

0.25
quart translates to a finite "decimal" number in binary (0.01 I think).

The point is: representing should-be integers as floats makes you loose
precision (negligable probalby but still...).

4)
> |> So why not work with int(float * 100) instead? This way you only have
> |> to take care of roundoffs etc when dividing.
>
> And multiplying, and calling most mathematical functions.

You are correct of course. My mistake.

But, the multiplication is exact before you start rounding off - I wont
start counting ordos for int*int vs. float*float, but it could have
some advantages
>>> a

11122
>>> b

22233
>>> a*b

247275426
>>> (a*b/10000,a*b%10000)

(24727, 5426)
On the other hand you will quickly loose accuracy if you perform
multiple multiplications or divisions or use other mathematical
functions.

5) So, when could this way of thinking be useful? Well, rarely, but if
you only add/subtract "decimals" and/or multiply "decimals" with whole
numbers or if you want to use some non-metric system to do scientific
stuff (compute square feet when having (feet1,inch1) * (feet2,inch2)
assuming that inches are atomic.)

This could of course be extended to
(feet, inch, quarter_of_afoot, sixteeth_of_a_foot) if you'd like - it
is all a matter of representation.

Regards,
Per
----
"It is a gift. A gift to the foes of 'the Terrorists'. Why not
use this 'terrorism'? Long has my father, 'George Bush Sr',
kept the forces of 'the terrorists' at bay. By the blood of
our people are your lands kept safe. Give 'the land of the
brave' the weapon of the enemy. Let us use it against him."

 
Reply With Quote
 
Scott David Daniels
Guest
Posts: n/a
 
      06-21-2006
Nick Maclaren wrote: (of fixed point)
> .... I am (just) old enough to remember when it was used for
> numeric work, and to have used it for that myself, but not old enough
> to have done any numeric work using fixed-point hardware.


You are using fixed point hardware today. Fixed point tracked the
"decimal point" (or "binary point" or whatever) in the mind of the
programmer, not in the state of the hardware. There never was
"fixed point hardware," it was simply a way of viewing the results
of the gates of the same hardware we use today.

--
--Scott David Daniels
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Nick Maclaren
Guest
Posts: n/a
 
      06-21-2006

In article <44997ea8$(E-Mail Removed)>,
Scott David Daniels <(E-Mail Removed)> writes:
|> Nick Maclaren wrote: (of fixed point)
|> > .... I am (just) old enough to remember when it was used for
|> > numeric work, and to have used it for that myself, but not old enough
|> > to have done any numeric work using fixed-point hardware.
|>
|> You are using fixed point hardware today. Fixed point tracked the
|> "decimal point" (or "binary point" or whatever) in the mind of the
|> programmer, not in the state of the hardware. There never was
|> "fixed point hardware," it was simply a way of viewing the results
|> of the gates of the same hardware we use today.

Er, no. Analogue hardware never was fixed-point, and I have used that

More relevantly, you are using a very parochial and unusual interpretation
of the word 'hardware'. In normal usage, it includes everything below
the instruct set specification (now often called the ABI). And I can
assure you that there is a considerable difference between the hardware
that provides a floating-point interface at the ABI and that which
provides a fixed-point interface.


Regards,
Nick Maclaren.
 
Reply With Quote
 
rasonage rasonage is offline
Junior Member
Join Date: Dec 2011
Posts: 1
 
      12-17-2011
floatVal = .4350001
"%.2f" % floatVal

To get .43 works great, but what if I wanted to have the decimal of what I truncate to be of variable input?
it's not like it would work this way:

accuracy = 2
floatVal = .4350001

"%.%if" %(floatVal, min_accuracy)

I'm trying to get the pointer "accuracy" as a value that can be changed/entered via user input so users can change what decimal place to round to. How would I do that?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
how to convert from Decimal('1.23456789') to Decimal('1.234') valpa Python 11 03-24-2009 07:11 AM
Error: Cannot convert Decimal("0.0000") to Decimal Vitaliy Python 1 05-29-2008 10:36 AM
TypeError: unsupported operand type(s) for -: 'Decimal' and 'Decimal'. Why? Gilbert Fine Python 8 08-01-2007 01:58 AM
Decimal to Packed Decimal Conversion in C++ Ven C++ 3 08-01-2006 03:56 PM



Advertisments