Skip to main content
hic
Former Employee
Former Employee

 

If you use equality as a condition when comparing floats, I will flunk you!

I can still hear the words of the Professor in my first programming class when studying for my engineering degree. The threat was very real – he meant it – and the reason was of course the fact that you cannot (always) represent decimal numbers in an exact binary form.

For example, we would never dream of writing a condition

  If( x = 0.3333333 , … )

when we want to test if x equals a third. Never. Because we know that a third cannot be represented exactly as a decimal number. No matter how many threes we add to the number, it will still not be exact.

But it is not uncommon that people make comparisons with an exact decimal number, similar to

      If( x = 0.01 , … )

thinking that it is a valid comparison, although it leads to exactly the same problem as the previous comparison! This becomes obvious if you look at the hexadecimal representation of 0.01:

      0.01 (decimal) = 0.028F5C28F5C28F…. (hex)

The sequence …28F5C… is repeated an infinite number of times, but since QlikView uses a finite number of binary digits (all according to the IEEE standard), QlikView will internally use a “rounded” number.

So what are the consequences? Well, QlikView will sometimes deliver the “wrong” number as result. Examples:

Ceil( 0.15, 0.01 ) will return 0.16
Floor( 0.34, 0.01 ) will return 0.33
0.175*1000 = 175 will return FALSE
Time( Floor( Time#( '04:00:00' ),1/24/60/60 )) will return 03:59:59

What you see are not errors in QlikView. And they are not errors in IEEE 754. Rather, they represent errors in the expectation and usage of binary floating point numbers. Once you understand what binary floating point numbers really are, it makes perfect sense. It's simply that some values cannot be exactly represented as binary numbers, so you get rounding errors. There's no way around it.

Should you want to investigate this yourself, I suggest you start with the following script that generates 100 numbers and their rounded counterparts. In five cases the Ceil() function rounds "incorrectly" and generates a "Diff" different from zero:

Load
   Num(Rounded,'(HEX) 0.000000000000000','.',' ') as RoundedHEX,
   (Round(100*Rounded) - PartsPer100)/100 as Diff,
   *;
Load
   Ceil(PartsPer100/100, 0.01) as Rounded,
   *;
Load
   RecNo() as PartsPer100
   Autogenerate 100 ;

So, what should you do?

First of all, you should realize that the rounding errors are small and usually insignificant. In most cases they will not affect the result of the analysis.

Further, you could avoid rounding with Floor() and Ceil() to sub-integer fractions.

Also, you could convert the numbers to integers, because the errors will only appear if the numbers can have sub-integer components. For instance, if you know that you always deal with dollars and cents, you could convert the numbers to (integer) cents:

  Round( 100*Amount ) as Cents

Or if you know that you never deal with time units smaller than seconds:

  Round( 24*60*60*Time#( Time, 'hh:mm:ss' ) ) as Seconds

And finally, you should never use equality as a condition when comparing floats. Use greater than or less than. My professor isn’t here to flunk you, but rest assured: In his absence, QlikView will do it for him.

HIC

39 Comments
hugmarcel
Specialist
Specialist

Hi

I am using round(value,maketime(0,0,1)), to circumvent some time calculating problems, as stated in

http://community.qlik.com/thread/58513

- Marcel

0 Likes
3,673 Views
juraj_misina
Luminary Alumni
Luminary Alumni

OK. Can someone please explain this?

Round((0.65)*100, 10)//=70

Round((0.35+0.3)*100, 10)// =60

Round(0.65, 0.1)//=0.7

Round(0.35+0.3, 0.1)// =0.6

0 Likes
3,673 Views
pljsoftware
Creator III
Creator III

Hello Juraj,

I think the problem is binary rappresentation of 0.3.

To test it use the num(0.3, '(bin)').

Best regards

Luca Jonathan Panetta

0 Likes
3,673 Views
marcus_sommer

It's quite clear 0.35 + 0.3 < 0.65 because of the rounding errors or respectively rounding blur - probably something like 0.64999999999999999?. In my above mentioned link it will be very clear explained why computers with which type of internal number-processing must be differ from a pure mathematically calculation.

I think if you checked: if(0.65 = 0.35 + 0.3, true(), false()) you will get a false.

- Marcus

0 Likes
3,673 Views
JonasValleskog
Partner - Creator
Partner - Creator

num((0.30+0.35)*100,'0.000000000000000')

displays the output

64.999999999999986

0 Likes
3,694 Views
Not applicable

Good one.

0 Likes
3,694 Views
johnw
Champion III
Champion III

I know I'm two years late to this, and as a non-employee, perhaps I shouldn't answer questions posed to Qlik / Henric, but...

"...other technology platforms..."

May have similar problems. Try this in Excel:

=ceiling(100.01-100.00,.01)

The right answer is .01. Excel returns .02.

"...surely providing the user the option to select a rounding method and therefore controlling their preferred method is the easiest and simplest solution?"

I fully agree that QlikView should allow us to specify the rounding method, and that doing so should be easy and simple.

But the errors you're seeing are not a problem with the round() function itself. It's a property of the internal representation of numbers in QlikView. Qlik can't make the round() function work properly on decimal numbers without also changing the internal representation of numbers to use base 10 instead of base 2.

That's not easy. That's not simple. And it would probably also have dramatic consequences for performance, based on the assumption that most servers process double precision binary floating point calculations much faster than decimal floating point calculations (with a floating point unit, for instance). If I were in charge at Qlik when they decided to use IEEE 754, I'd have probably made the same decision.

And even if QlikView did change to a decimal format, the round() function would technically still not be correct. It would return correct results on decimal numbers, yes. But it still couldn't handle base 3 numbers, for instance. Nobody's auditing financial statements in base 3, though, so it's admittedly much less of a practical problem.

"When trying to explain to [auditors] that its a Qlikview issue because they use floating points that comply with IEEE 754 etc, they simply and frankly don't care... they expect that the 2 should balance - being 1c out is simply not acceptable."

Agreed. Absolutely. Completely. If I were an auditor, and someone tried to use "we're using IEEE 754" as an excuse, I would come down on them HARD. "You're doing WHAT?! Are you KIDDING me? You can't do your financials in IEEE 754!!!"

So what DO you do?

I think the RIGHT thing to do is to handle your financial reporting in a system that uses a decimal representation, i.e., not QlikView. (Sorry, Qlik. I love y'all. I love QlikView. It's ubiquitous in our company. It even has a lot of our financial data in it. But that freaks me out. Double when an auditor asks me for information.)

A fairly PRACTICAL thing to do, if you're already using QlikView for auditable financial reporting and will continue doing so, is to convert all numbers to integers before loading them into QlikView. Binary floating point, and thus QlikView functions like round(), have 100% accuracy on integer values. (We've not done this. We're still storing in dollars and hoping for the best.)

And you may be able to calm down your aggravated auditor. For instance, maybe I just yelled at you as indicated above. Tell me firmly, "We are NOT doing our financials in IEEE 754. All the REAL financial processing is handled outside of QlikView. It's all done in Financial Tool XYZ, which uses decimal. THAT is what we use for our books. THAT is how we handle our taxes. QlikView is not our system of record. In regards to the financials you see in it, we are using it ONLY as a decision support tool. We do a billion dollars of business a year. We don't need cent or even dollar accuracy within our decision support tool in order to make the right decisions. We only need complete accuracy for our underlying financials, and we HAVE it for our underlying financials. So calm down."

"Lastly, providing a workaround by adding a small number to the result is complex and messy and it still seems like the results are not guaranteed."

The guaranteed fix is converting all numbers to integers before loading them into QlikView, and using only integer values in QlikView.

"...providing a couple more functions for the user to control rounding would put this issue to rest, this is surely something that is very easy for you guys to do..."

I fully support optional specification of rounding methods. It should be very easy to do that. But it won't put this issue to rest, because it won't fix the actual error. The round() function isn't where the problem lies.

3,694 Views
ecolomer
Master II
Master II

Very nice and clear post

Thnak's for sharing

Saludos

Enrique

0 Likes
3,694 Views
beck_bakytbek
Master
Master

Thanks for your sharing,

as always very informative,

Henric, i have one question, namely if create a gauge: i can Show a right number with right rounding (for instance: 94,45 % ), but on the surface: i mean : Text as Pop up, i see always 95 %.

how can i fix it this Deviation?

thanks a lot in advance

beck

0 Likes
3,694 Views
hic
Former Employee
Former Employee

Usually you can do this and similar things by using two measures, with the same formula in both. Only the first one is used for the gauge.

Below I have different number of decimals in the two measures, and only the second one is active as pop-up. (You may need to remove the "Pop-up labels" check mark on the Presentation tab, also.)

Image1.png

Image2.png

3,694 Views