- Posts: 5
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Incorrect rounding of numbers by LimeSurvey
- survep
- Topic Author
- Offline
- New Member
Less
More
2 years 4 months ago #222549
by survep
Incorrect rounding of numbers by LimeSurvey was created by survep
Hi!
It seems that using the "round()" command sometimes leads to wrong results.
Simple example (see also attachement): If you program a question with "{100-round(14.28*7,2)}" as text to be displayed, LimeSurvey displays "0.040000000000006" although the correct number would be "0.04".
I stumbled upon this because I programmed a survey where people enter a number that is later used in different calculations (of which the the results are displayed to the participant). So this problem remains if you work with a number that was entered by a partipant and it also remains if you store each step of the calculation in a separate question/equation. The weird thing is that it seems to happen only for very specific combinations. If you do the calcuation in the simple example above with 14.27 instead of 14.28 for example, the correct result is shown. If you use 14.281 instead of 14.28 the result shown is still incorrect.
Am I missing something here? Any help is greatly appreciated. I used LimeSurvey Version 5.2.2 to program the example but the problem also exists for Version 3.22.1+200129.
It seems that using the "round()" command sometimes leads to wrong results.
Simple example (see also attachement): If you program a question with "{100-round(14.28*7,2)}" as text to be displayed, LimeSurvey displays "0.040000000000006" although the correct number would be "0.04".
I stumbled upon this because I programmed a survey where people enter a number that is later used in different calculations (of which the the results are displayed to the participant). So this problem remains if you work with a number that was entered by a partipant and it also remains if you store each step of the calculation in a separate question/equation. The weird thing is that it seems to happen only for very specific combinations. If you do the calcuation in the simple example above with 14.27 instead of 14.28 for example, the correct result is shown. If you use 14.281 instead of 14.28 the result shown is still incorrect.
Am I missing something here? Any help is greatly appreciated. I used LimeSurvey Version 5.2.2 to program the example but the problem also exists for Version 3.22.1+200129.
Attachments:
The topic has been locked.
- Joffm
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 12916
- Thank you received: 3975
2 years 4 months ago - 2 years 4 months ago #222550
by Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic Incorrect rounding of numbers by LimeSurvey
Hi,
this is not the rounding.
You see, the simple "100-99.96" has the same result.
Floating-point arithmetic and the IEEE 754 specification
Joffm
this is not the rounding.
You see, the simple "100-99.96" has the same result.
Floating-point arithmetic and the IEEE 754 specification
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 2 years 4 months ago by Joffm.
The following user(s) said Thank You: DenisChenu, survep
The topic has been locked.
- survep
- Topic Author
- Offline
- New Member
Less
More
- Posts: 5
- Thank you received: 0
2 years 4 months ago #222553
by survep
Replied by survep on topic Incorrect rounding of numbers by LimeSurvey
Hi
Thanks a lot for your fast reply! Okay, I see it has nothing to do with the rounding but that it applies more generally.
I must admit I am not familiar with floating-point arithmetic and the IEEE 754 specification. I did some research and it seems some rounding error is inevitable. Is there any way to deal with this more generally in LimeSurvey? If some calculation is involved, it seems there might always be cases where the calculation is not "exact" and leads to wrong restults. How to avoid this? (If the number to be displayed should include no more than a certain number of decimal places one could of course always just apply the rounding command to the final result, but what if exact results should be displayed without any rounding involved?)
Thanks a lot for your fast reply! Okay, I see it has nothing to do with the rounding but that it applies more generally.
I must admit I am not familiar with floating-point arithmetic and the IEEE 754 specification. I did some research and it seems some rounding error is inevitable. Is there any way to deal with this more generally in LimeSurvey? If some calculation is involved, it seems there might always be cases where the calculation is not "exact" and leads to wrong restults. How to avoid this? (If the number to be displayed should include no more than a certain number of decimal places one could of course always just apply the rounding command to the final result, but what if exact results should be displayed without any rounding involved?)
The topic has been locked.
- DenisChenu
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 13637
- Thank you received: 2491
2 years 4 months ago #222568
by DenisChenu
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
Replied by DenisChenu on topic Incorrect rounding of numbers by LimeSurvey
round(100-14.28*7,2) maybe ?
Add the round at end of formula,
Wrap formula with round.
Add the round at end of formula,
Wrap formula with round.
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
- survep
- Topic Author
- Offline
- New Member
Less
More
- Posts: 5
- Thank you received: 0
2 years 4 months ago #222844
by survep
Replied by survep on topic Incorrect rounding of numbers by LimeSurvey
Thank you for the suggestion. That's of course possible when it is clear how many decimal places one wants to have at most (see also my last sentence in previous post). But it does not help if I want to report exact results (that differ in the length of their decimal places).
The topic has been locked.