Erik Thysell
Erik Thysell

Reputation: 1352

Fortran: is 0.d0 always == 0?

In fortran, can I always count on 0.d0 == 0 to result in .true. ? I.e.:

real(8) :: x
integer :: i

x = 0.d0
i = 0
write(*,*) x == i

I have tried in a simple piece of code and it turns out as .true. but can I always trust this? The reason for this is I want to avoid problems with acos(x) if x < -1.

Upvotes: 3

Views: 5784

Answers (1)

Bathsheba
Bathsheba

Reputation: 234635

i will be promoted to the floating point type before application of the relational equality test ==.

A floating point zero will compare true with itself, and also with a negative signed zero.

"Can I always trust this?" Yes, you can.

Upvotes: 3

Related Questions