I have a float variable-field1
in 2 tables-table1
& table2
. When I query the table and check the values of the field both look identical but when I find their difference it gives a difference instead of zero.
Field1(Table1) value---84.4660194174757
Field2(Table2) value---84.4660194174757
Differnce---1.4210854715202E-14
Why would I get this problem?
Yes if they are Float objects.
The compare() method of Float Class is a built-in method in Java that compares the two specified float values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call. Parameters: The function accepts two parameters: f1: The first float value to be compared.
Hence, this is equivalent to: declare @num1 decimal(18, 0) = 1.98; declare @num2 decimal(18, 0) = 2.2; SQL Server then assigns the values by converting the constants to the appropriate value, and both are being set to "2.". You need to explicitly set the precision/scale if you want those values to be stored exactly.
You can easily compare variables using INTERSECT as it is NULL -sensitive: DECLARE @A BIT = NULL ,@B BIT = 1; IF EXISTS ( SELECT @A INTERSECT SELECT @B ) SELECT 'equal'; ELSE SELECT 'not equal'; Also, when you need to do such comparisons in complex queries, this could improve performance as it allows using indexes.
ROUND
to limit the decimal placesABS(value1-value2) < 0.00001
with some suitable valueIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With