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

Re: Getting fractional part from a float without using stringoperations

On Wed, Nov 19, 2008 at 10:01 PM, srinivasan srinivas
<sr*********@yahoo.co.inwrote:
Yes it works for most of the cases. But it doesn't for the following case:
>>>str(abs(int(1234567.89)-1234567.89))
'0.889999999898'
Since you really care about significant figures here, have you
considered using decimal rather than float as, IIRC, it handles this
correctly?

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
>
Thanks,
Srini
----- Original Message ----
From: Tino Wildenhain <ti**@wildenhain.de>
To: srinivasan srinivas <sr*********@yahoo.co.in>
Cc: Jeremiah Dodds <je************@gmail.com>; py*********@python.org
Sent: Wednesday, 19 November, 2008 7:33:46 PM
Subject: Re: Getting fractional part from a float without using string operations

srinivasan srinivas wrote:
>Yes. But it didn't give only the expected decimals.
For ex:
> >>a = 1.23
abs(int(a) -a)
0.22999999999999998
I would like to get the result '0.23' only.

well, thats what get stored internally - there
is no way around it if you are using floating
point numbers:
>>>0.23
0.23000000000000001

but str() handles the rounding correctly:
>>>print 0.23
0.23
>>>print abs(int(a) -a)
0.23

See also http://en.wikipedia.org/wiki/Floating_point
for the problems with FP figures.

Regards
Tino

Get perfect Email ID for your Resume. Grab now http://in.promos.yahoo.com/address
--
http://mail.python.org/mailman/listinfo/python-list
Nov 20 '08 #1
0 2323

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

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
5
by: Michael Søndergaard | last post by:
I Pascal there are a function for retriving the fractional part of a real number (Frac), I can't find any in the dotnet framework. The function needs to remove the integer part and keep only the...
5
by: Steve Peterson | last post by:
Hello I have a method that returns a decimal. I need to get the lowest integer value from this decimal. For example from "4.9764" I need to get "4", or from "13.134" I need "13". Is there a .Net...
5
by: ME | last post by:
What is the fastest way of getting the fraction part of a floating point number? So, if I have a float 10.3 I want to get a float with value 0.3 fast. Thanks
2
by: djzbor | last post by:
Any who can help!! let i have a number 16.0230045 i want to count the fractional part of this number means i want to get the number of digits of the fractional part of this number and store each...
2
by: Bob | last post by:
Greetings All, Is there a simple way of suppressing the fractional part of a timespan At the moment I use one time span to populate another minus the fractional part .. this is a bit clunky. ...
10
by: schaefer.mp | last post by:
Does anyone know of an approximation to raising a negative base to a fractional exponent? For example, (-3)^-4.11111 since this cannot be computed without using imaginary numbers. Any help is...
12
by: bsabiston | last post by:
Hi, I'm trying to get the fractional part of a floating point number. I've tried fmod() and modf(), and while both do work, they also occasionally return 1.0 for the fractional part of the number...
1
by: srinivasan srinivas | last post by:
Yes it works for most of the cases.  But it doesn't for the following case: '0.889999999898' Thanks, Srini ----- Original Message ---- From: Tino Wildenhain <tino@wildenhain.de>
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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: 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...

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.