Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong output of the intrinsic function fraction() in Fortran

I do not understand the output of FRACTION(), for example

write(*,*) fraction(553.334)

It gives me 0.5403652.

I thought it should return 0.334, as 553.334-floor(553.334) does. What is wrong in my understanding?

like image 923
bhqasx Avatar asked Dec 09 '22 05:12

bhqasx


1 Answers

Au contraire, that value is exactly (within the limits of precision available) the right answer. I think the error is that you have misunderstood what fraction does. To paraphrase the standard (widely available online, as are many tutorials and other guides) fraction(x) returns the fractional part of the model representation of x. The model representation of x is, on most modern computers, a number of the form x*2^-k. In effect the function returns the number x divided by the next highest power of 2, in the case of your example 553.034/1024 == 553.034*2^-10.

As ever in computing, RTFM (Read The Fortran Manual).

like image 73
High Performance Mark Avatar answered Mar 14 '23 23:03

High Performance Mark