Nokia sites use cookies to improve and personalize your experience and to display advertisements. The sites may also include cookies form third parties. By using this site, you consent to the use of cookies. Learn more

# Thread: Floating point

1. Hello,

What's the best practice for using floating point numbers in pys60 1.9.2? For example division of two integer values converted to float does not give a sensible result (float(12345)/n gives 2.46..).

Using decimal seems to solve the problem partially, but printing the values also sometimes outputs results in the same way (and decimal is much slower).

Any suggestions how to operate floating point numbers retaining reasonable accuracy?

2. A few examples of what the result is and what you want the result to look like might help us understand the situation better.

3. Basically, having a list of integer values I need to find mean and standard deviation of them.

Example -
[3597, 3473, 3454, 3444, 3444, 3444, 3570, 3689, 3717, 3617, 3521, 3406, 3429, 3418, 3533, 3454, 3454, 3454, 3454, 3329, 3442, 3419, 3549, 3549, 3474, 3474, 3474, 3572, 3534, 3521, 3521, 3557, 3549, 3413, 3413, 3513, 3500, 3534, 3549, 3549, 3534, 3500, 3489, 3618, 3627, 3500, 3617, 3481, 3481, 3481, 3500, 3651, 3500, 3500, 3500, 3500, 3500, 3534, 3534, 3534, 3534, 3534, 3534, 3442, 3557, 3582, 3582, 3582, 3557, 3572, 3572, 3549, 3534, 3534, 3528, 3528, 3528, 3513, 3500, 3481, 3500, 3500, 3521, 3521, 3521, 3521, 3521, 3544, 3557, 3557, 3557, 3557, 3572, 3557, 3557, 3557, 3674, 3557, 3674, 3544]

Using float function mean gives "3.52.." (where it should be 3524)
And stdev gives "63..737663375194" (should be 63.0737663375194)

The values actually seem to be the same, but I can't find a way to print them out in a readable way (let's say 3524.0000 and 63.0738).
One possible way is to use decimal module, but it seems to be much slower even than floating point.

mean and stdev defined as:

def mean(data):
n = len(data)
sum = 0
for item in data:
sum = sum+item
return float(sum)/n

def stdev(data, mean):
n = len(data)
sum = 0
for item in data:
sum = sum+(item-mean)*(item-mean)
t = sum/n
return sqrt(t)

stdev is called stdev(data, mean(data))

4. Originally Posted by AndriusA
And stdev gives "63..737663375194" (should be 63.0737663375194)
Looking at the double dot I am guessing this is related to the OpenC bug which was present one of the old 1.9.x release. Can you please upgrade to the latest PyS60 1.9.5 release and then check?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.