Comparing a float and its copy

Abhinav

I know that comparing two doubles is problematic if they are obtained from different calculations. But does this also hold in the case when one of them is a copy (in value) of the other. The following lines explain the scenario. If I have a problem like this,

double a,b;
a=randdouble();/*some double value*/
b=a;

Then,

Q1) Is the comparison a==b always guaranteed to return true in case of the C compiler (i have gcc 6.1.1)?

Q2) Would the above answer remain the same if I am allocating variables a and b in heap memory using malloc?

Q3) Would the above answers remain the same if I replace C compiler with a JAVA compiler (I am using Open JDK 1.7.0) with the necessary syntax changes ofcourse.

Edit 1 : The numbers a and b are != NaN

chqrlie

Q1: The comparison is not guaranteed to evaluate to true, for the simple reason that NaN compares unequal to itself. There might be other cases too, but NaN is an obvious counterexample.

Q2: It makes no difference where the variables reside in memory.

Q3: I would expect Java to behave similarly.

This special case aside, I do believe the Standard does not give such a guarantee:

Imagine an ABI where double expressions are evaluated and values are returned with 80 bit precision (Intel 80x87 stack) but stored as 64 bit IEEE-754 doubles. Even if randdouble() is defined to return a double, as opposed to long double, its return value may be more precise than the value stored into a or b. Depending on how the compiler optimises the various expressions between the randdouble() function call and the comparison a == b, it may end up comparing the 80 bit precise return value with its close cousin obtained by converting to 64 bit and back to 80 bit. The comparison would fail if precision was lost in the conversion. I shall try and find a proper reference from the Standard to support this, but it seems plausible, and albeit whether a or b are local variables or stored on the heap might make a difference in the sequence of conversions performed, it would still be ill-advised to assume any guarantee for one or the other situations.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Comparing a float and its copy

From Dev

excel sheet Comparing two different columns if match copy its next two cells into new column

From Dev

Identical float values comparing as inequal

From Dev

Why comparing float values is such difficult?

From Dev

python comparing elements in dictionary to a float

From Dev

Integer not comparing equal to its value

From Dev

Comparing Python Decimals created from float and string

From Dev

Strange behaviour when comparing cast float to zero

From Dev

comparing reducible fractions using float/double division

From Dev

pandas comparing series with numpy float gives TypeError

From Dev

pandas comparing series with numpy float gives TypeError

From Dev

Matlab - comparing multidimensional double/float arrays (isequal)

From Dev

comparing name of gameobject to its parent name

From Dev

copy data after comparing a specific value

From Dev

Sorting a tuple by its float element

From Dev

Printing a Float Beyond Its Precision

From Dev

Printing a Float Beyond Its Precision

From Dev

how to copy a value from float? to float, and put zero if the float? is null?

From Dev

Copy Azure Blob and its metadata

From Dev

Copy element with its properties in mootools

From Dev

Does emit copy its arguments?

From Dev

Does emit copy its arguments?

From Dev

Copy element with its properties in mootools

From Dev

Copy node and alter its content

From Dev

puppet copy directory but not its content

From Dev

About comparing an integer and a float/double in C/C++

From Dev

Random unable to deduce correct type when comparing the value to a float

From Dev

Why does comparing two equal Float values with == returns false

From Dev

MatLab - How to accept a tolerance when comparing two float numbers

Related Related

  1. 1

    Comparing a float and its copy

  2. 2

    excel sheet Comparing two different columns if match copy its next two cells into new column

  3. 3

    Identical float values comparing as inequal

  4. 4

    Why comparing float values is such difficult?

  5. 5

    python comparing elements in dictionary to a float

  6. 6

    Integer not comparing equal to its value

  7. 7

    Comparing Python Decimals created from float and string

  8. 8

    Strange behaviour when comparing cast float to zero

  9. 9

    comparing reducible fractions using float/double division

  10. 10

    pandas comparing series with numpy float gives TypeError

  11. 11

    pandas comparing series with numpy float gives TypeError

  12. 12

    Matlab - comparing multidimensional double/float arrays (isequal)

  13. 13

    comparing name of gameobject to its parent name

  14. 14

    copy data after comparing a specific value

  15. 15

    Sorting a tuple by its float element

  16. 16

    Printing a Float Beyond Its Precision

  17. 17

    Printing a Float Beyond Its Precision

  18. 18

    how to copy a value from float? to float, and put zero if the float? is null?

  19. 19

    Copy Azure Blob and its metadata

  20. 20

    Copy element with its properties in mootools

  21. 21

    Does emit copy its arguments?

  22. 22

    Does emit copy its arguments?

  23. 23

    Copy element with its properties in mootools

  24. 24

    Copy node and alter its content

  25. 25

    puppet copy directory but not its content

  26. 26

    About comparing an integer and a float/double in C/C++

  27. 27

    Random unable to deduce correct type when comparing the value to a float

  28. 28

    Why does comparing two equal Float values with == returns false

  29. 29

    MatLab - How to accept a tolerance when comparing two float numbers

HotTag

Archive