A program was going wrong, and found
FRACTION(3.75)=0.9375 and FRACTION(2.5)=0.9375
A program was going wrong, and found
FRACTION(3.75)=0.9375 and FRACTION(2.5)=0.9375
A program was going wrong, and found
FRACTION(3.75)=0.9375 and FRACTION(2.5)=0.9375
On 8/13/2025 9:21 PM, Woozy Song wrote:
A program was going wrong, and found
FRACTION(3.75)=0.9375 and FRACTION(2.5)=0.9375
I tried it with gfortran version 15, just for fun, and I got the same result.
A program was going wrong, and found
FRACTION(3.75)=0.9375 and FRACTION(2.5)=0.9375
On Thu, 14 Aug 2025 11:21:00 +0800, Woozy Song wrote:
A program was going wrong, and found
FRACTION(3.75)=0.9375 and FRACTION(2.5)=0.9375
Broken as designed, according to the spec.
Why on Earth does FRACTION have this meaning?
On Fri, 15 Aug 2025 22:24:52 +0000, Lawrence D’Oliveiro wrote:
On Thu, 14 Aug 2025 11:21:00 +0800, Woozy Song wrote:
A program was going wrong, and found
FRACTION(3.75)=0.9375 and FRACTION(2.5)=0.9375
Broken as designed, according to the spec.
Why on Earth does FRACTION have this meaning?
To what spec are you referring?
gfortran is giving the correct answer (although
I suspect OP has a transcription problem with
the second example).
On 8/16/2025 7:02 PM, Steven G. Kargl wrote:
On Fri, 15 Aug 2025 22:24:52 +0000, Lawrence D’Oliveiro wrote:
On Thu, 14 Aug 2025 11:21:00 +0800, Woozy Song wrote:
A program was going wrong, and found
FRACTION(3.75)=0.9375 and FRACTION(2.5)=0.9375
Broken as designed, according to the spec.
Why on Earth does FRACTION have this meaning?
To what spec are you referring?
gfortran is giving the correct answer (although
I suspect OP has a transcription problem with
the second example).
I think most casual, non-math, non-floating point experts would expect this:
I think most casual, non-math, non-floating point experts would expect
this:
"The fractional part of a real number is the decimal portion of the
number, excluding the integer part. It represents the difference between
the real number and its integer part.
On Sat, 16 Aug 2025 20:40:13 -0500, Gary Scott wrote:
I think most casual, non-math, non-floating point experts would expect
this:
"The fractional part of a real number is the decimal portion of the
number, excluding the integer part. It represents the difference between
the real number and its integer part.
Sure. Except that computers typically do not represent floating-point
numbers in decimal, but in binary. This is the significance of the “model representation” phrase in the language spec.
The way you describe is obviously the most natural interpretation, these days. And most other languages do it that way. But not Fortran.
On Sat, 16 Aug 2025 20:40:13 -0500, Gary Scott wrote:
I think most casual, non-math, non-floating point experts would expect
this:
"The fractional part of a real number is the decimal portion of the
number, excluding the integer part. It represents the difference between
the real number and its integer part.
Sure. Except that computers typically do not represent floating-point numbers in decimal, but in binary. This is the significance of the “model representation” phrase in the language spec.
The way you describe is obviously the most natural interpretation, these days. And most other languages do it that way. But not Fortran.
On Sat, 16 Aug 2025 20:40:13 -0500, Gary Scott wrote:
On 8/16/2025 7:02 PM, Steven G. Kargl wrote:
On Fri, 15 Aug 2025 22:24:52 +0000, Lawrence D’Oliveiro wrote:
On Thu, 14 Aug 2025 11:21:00 +0800, Woozy Song wrote:
A program was going wrong, and found
FRACTION(3.75)=0.9375 and FRACTION(2.5)=0.9375
Broken as designed, according to the spec.
Why on Earth does FRACTION have this meaning?
To what spec are you referring?
gfortran is giving the correct answer (although
I suspect OP has a transcription problem with
the second example).
I think most casual, non-math, non-floating point experts would expect this:
For someone programming in the Fortran language, I would expect
that they would consult some form of documentation to learn why
their expectation might be wrong. For example, the gfortran
documentation (which comes with the compiler) contains
8.119 ‘FRACTION’ -- Fractional part of the model representation ===============================================================
_Synopsis_:
‘Y = FRACTION(X)’
_Description_:
‘FRACTION(X)’ returns the fractional part of the model
representation of ‘X’.
_Class_:
Elemental function
_Arguments_:
X The type of the argument shall be a ‘REAL’.
_Return value_:
The return value is of the same type and kind as the argument. The
fractional part of the model representation of ‘X’ is returned; it
is ‘X * RADIX(X)**(-EXPONENT(X))’.
_Example_:
program test_fraction
real :: x
x = 178.1387e-4
print *, fraction(x), x * radix(x)**(-exponent(x))
end program test_fraction
Am 8/17/25 um 05:18 schrieb Steven G. Kargl:
On Sat, 16 Aug 2025 20:40:13 -0500, Gary Scott wrote:
On 8/16/2025 7:02 PM, Steven G. Kargl wrote:
On Fri, 15 Aug 2025 22:24:52 +0000, Lawrence D’Oliveiro wrote:
On Thu, 14 Aug 2025 11:21:00 +0800, Woozy Song wrote:
A program was going wrong, and found
FRACTION(3.75)=0.9375 and FRACTION(2.5)=0.9375
Broken as designed, according to the spec.
Why on Earth does FRACTION have this meaning?
To what spec are you referring?
gfortran is giving the correct answer (although
I suspect OP has a transcription problem with
the second example).
I think most casual, non-math, non-floating point experts would expect this:
For someone programming in the Fortran language, I would expect
that they would consult some form of documentation to learn why
their expectation might be wrong. For example, the gfortran
documentation (which comes with the compiler) contains
8.119 ‘FRACTION’ -- Fractional part of the model representation
===============================================================
_Synopsis_:
‘Y = FRACTION(X)’
_Description_:
‘FRACTION(X)’ returns the fractional part of the model
representation of ‘X’.
_Class_:
Elemental function
_Arguments_:
X The type of the argument shall be a ‘REAL’.
_Return value_:
The return value is of the same type and kind as the argument. The
fractional part of the model representation of ‘X’ is returned; it >> is ‘X * RADIX(X)**(-EXPONENT(X))’.
_Example_:
program test_fraction
real :: x
x = 178.1387e-4
print *, fraction(x), x * radix(x)**(-exponent(x))
end program test_fraction
Thank you, Steve.
The following program, adapted from the Example in the documentation you cite:
program test_fraction
real :: x
x = 3.75
print *, fraction(x), x
print *, x * radix(x)**(-exponent(x))
end program test_fraction
prints:
0.937500000 3.75000000
0.00000000
but I was expecting it to print:
0.937500000 3.75000000
0.937500000
because the documentation says that fraction(x) is the same as x * radix(x)**(-exponent(x)) .
I suggest that the documentation is wrong, and should say
' ... model representation of ‘X’ is returned; it is ‘X * REAL(RADIX(X))**(-EXPONENT(X))’.
Concerning the OP's finding that FRACTION(2.5) is printed as 0.9375:
I cannot reproduce that with gfortran 11.5.0 or 13.3.1 or 14.2.1 which all correctly calculate 0.625 .
So to me this is a documentation bug, and possibly a bug in gfortran 12.2 .
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 1,064 |
Nodes: | 10 (0 / 10) |
Uptime: | 149:58:50 |
Calls: | 13,691 |
Calls today: | 1 |
Files: | 186,936 |
D/L today: |
438 files (115M bytes) |
Messages: | 2,410,967 |