Rounding for math operations

Hi everyone,

We’ve added a fix to a longstanding known issue with math operations. Shoutout to @alex.stanescu for the engineering work for this change!

Whenever you use a number with decimals, Bubble uses the float data type behind the scenes. As a product of how computers work, floats don’t represent numbers exactly as you enter them. For instance, you may enter 3.5*100 with the expectation that you’d get 3500, but instead you’ll get 3500.0000000000013 since 3.5, as a float, is represented as 3.5000000000000013 on the computer’s end. For the curious, the reason is that floats are stored in exponential form with a significand of 53 binary digits and an exponent of 10 binary digits.

We realize this can become frustrating for users who are looking to perform simple math operations. Therefore, we’ve implemented a fix where we will round the final output of a mathematical calculation to 15 significant figures. We will not round intermediate calculations. This approach allows us to ensure we’re rounding to the mathematically correct result for the majority of calculations that our users perform.

To be explicit, final output represents the last-most result at which a number is a number type before it’s sent off to an API or converted to another non-number type for a calculation. These can include sub-expressions in a property editor field; they do not necessarily have to be the final result of a property editor field.

In terms of how this will affect you, you hopefully should see numbers rounding correctly more often for many of your math operations, so you don’t need to worry as much about using the :rounded to operator. Also, given that we’re rounding to a considerably high number of significant figures, you shouldn’t be seeing significant changes in the results of your math operations. For math operations that use extremely large numbers or involve complex calculations, the results you get will have the same precision as the results returned before.

In the case that you’re working a result where you’d like the number to return more than 15 significant digits, you can use :formatted as to specify how the final result will look. We will then return the unrounded number with however many decimal places you’d like.

We hope this will provide more reliability when you perform math operations in your app!

13 Likes

Great !!! Thanks !