1

I'm writing some python code that needs to convert a real to an integer:

Section of the relevant code:

for q in range(len(diff2FittedFunc[:])):
    moveBack=(10.0/len(diff2FittedFunc[:]))*q
    rampPosition = 10-moveBack 
    arrayPos=int(rampPosition*10)
    print rampPosition,arrayPos

I expected (and wanted) the list that is printed to output a list of values from 100 to 1 in increments of 1. After checking the list, I noticed that there are missing values and duplicate values e.g. 38.

Can anyone shed any light on why this is happening and how I can correct it?

(FYI: len(diff2FittedFunc[:] = 100)

Thank you very much in advance.

OUTPUT:

- 10.0 100
- 9.9 99
- 9.8 98
- 9.7 97
- 9.6 96
- 9.5 95
- 9.4 94
- 9.3 93
- 9.2 92
- 9.1 91
- 9.0 90
- 8.9 89
- 8.8 88
- 8.7 87
- 8.6 86
- 8.5 85
- 8.4 84
- 8.3 83
- 8.2 82
- 8.1 81
- 8.0 80
- 7.9 79
- 7.8 78
- 7.7 77
- 7.6 76
- 7.5 75
- 7.4 74
- 7.3 73
- 7.2 72
- 7.1 71
- 7.0 70
- 6.9 69
- 6.8 68
- 6.7 67
- 6.6 66
- 6.5 65
- 6.4 64
- 6.3 63
- 6.2 61
- 6.1 61
- 6.0 60
- 5.9 58
- 5.8 58
- 5.7 57
- 5.6 56
- 5.5 55
- 5.4 53
- 5.3 53
- 5.2 51
- 5.1 51
- 5.0 50
- 4.9 48
- 4.8 48
- 4.7 46
- 4.6 46
- 4.5 45
- 4.4 43
- 4.3 43
- 4.2 41
- 4.1 41
- 4.0 40
- 3.9 38
- 3.8 38
- 3.7 36
- 3.6 36
- 3.5 35
- 3.4 33
- 3.3 33
- 3.2 31
- 3.1 30
- 3.0 30
- 2.9 28
- 2.8 28
- 2.7 26
- 2.6 25
- 2.5 25
- 2.4 23
- 2.3 23
- 2.2 21
- 2.1 20
- 2.0 20
- 1.9 19
- 1.8 17
- 1.7 16
- 1.6 15
- 1.5 15
- 1.4 14
- 1.3 12
- 1.2 11
- 1.1 10
- 1.0 10
- 0.9 9
- 0.8 7
- 0.7 6
- 0.6 5
- 0.5 5
- 0.4 3
- 0.3 2
- 0.2 1
- 0.1 0
0

2 Answers 2

1

It's rounding the floats up but truncating the integers. Try this and it should show you the problem:

Try changing the last line to

print "{:.2f}".format(rampPosition),arrayPos

Sign up to request clarification or add additional context in comments.

2 Comments

I wasn't able to determine the solution to this problem by applying your line.
OK. Can you append the output you get when you run it with the line changed?
0

I would prefer doing such a task using string. I see the function as just removing the decimal. Here is my version of the function:

def func(real):
    temp=str(real)
    temp=list(temp)
    temp.remove(".")
    temp="".join(temp)
    return int(temp)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.