Incorrect rounding of numbers by LimeSurvey

1 month 2 weeks ago #222549 by survep

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.

Please Log in to join the conversation.

1 month 2 weeks ago - 1 month 2 weeks ago #222550 by 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


Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 1 month 2 weeks ago by Joffm.
The following user(s) said Thank You: DenisChenu, survep

Please Log in to join the conversation.

1 month 2 weeks ago #222553 by survep

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?)

Please Log in to join the conversation.

1 month 2 weeks ago #222568 by DenisChenu
round(100-14.28*7,2) maybe ?

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.

Please Log in to join the conversation.

1 month 1 week ago #222844 by survep
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).

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now