Delphi - comparison of two "real" numeric variables

I have a problem with matching two variables of type "Real". One of them is the result of a mathematical operation stored in the data set, the second is the value of the edit field in the form, converted by StrToFloat and stored in the "Real" variable. The problem is this: 121.97 not equal 121.97

As you can see, the program is trying to tell me that 121.97 is not equal to 121.97 ... I read this section , and I'm not sure if this is the same problem. If that were the case, it would not be both numbers stored in variables, how exactly is the exact same nearest representable number that is 121.96999 99999 99998 86313 16227 83839 70260 62011 71875 ?

Now let's say that they are not stored as the same representable number. How to find out exactly how they are stored? When I look in the "CPU" debug window, I am completely lost. I see the addresses where these values ​​should be, but nothing even looks like some kind of binary, hexadecimal or other representation of the actual number ... I admit that the extended debugging of an unknown universe to me ...

Edit: These two values ​​are really slightly different.

enter image description here

Well, I don’t need to understand everything. Although I do not have money with money, there will be a maximum of 3 decimal places, so "currency" is the way out

BTW: Calculation:

 DATA[i].Meta.UnUsedAmount := DATA[i].AMOUNT - ObjQuery.FieldByName('USED').AsFloat; 

In this case, it is 3695 - 3573.03

+6
source share
1 answer

For unknown reasons, you cannot view the float value (single / double or real48) as hexadecimal in the list of hours.

However, you can still view the hexadecimal representation by viewing it as a memory dump.
Here's how: Add a variable to the watchlist.
Right click on the watch β†’ Edit Watch ...
View it as a memory dump

enter image description here

Now you can compare the two values ​​in the debugger.

Never use floats for cash
You know, of course, that you should not use float to count money.
You will encounter all the troubles with rounding, and comparisons will not work as you wish.
If you want to work with money, use the currency type instead. It does not have these problems, supports 4 decimal places and can be compared using the = operator without rounding problems.

Your database uses the data type money or currency .

+10
source

All Articles