• FRACTION() seems broken in gfortran 12.2

    From Woozy Song@suzyw0ng@outlook.com to comp.lang.fortran on Thu Aug 14 11:21:00 2025
    From Newsgroup: comp.lang.fortran

    A program was going wrong, and found
    FRACTION(3.75)=0.9375 and FRACTION(2.5)=0.9375
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Thomas Koenig@tkoenig@netcologne.de to comp.lang.fortran on Thu Aug 14 16:47:19 2025
    From Newsgroup: comp.lang.fortran

    Woozy Song <suzyw0ng@outlook.com> schrieb:
    A program was going wrong, and found
    FRACTION(3.75)=0.9375 and FRACTION(2.5)=0.9375

    Do you have a self-contained example (something that can be compiled)?
    --
    This USENET posting was made without artificial intelligence,
    artificial impertinence, artificial arrogance, artificial stupidity,
    artificial flavorings or artificial colorants.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Lynn McGuire@lynnmcguire5@gmail.com to comp.lang.fortran on Thu Aug 14 15:44:58 2025
    From Newsgroup: comp.lang.fortran

    On 8/13/2025 10:21 PM, Woozy Song wrote:
    A program was going wrong, and found
    FRACTION(3.75)=0.9375 and FRACTION(2.5)=0.9375

    Have you tried upgrading to a newer version of gfrotran ?

    Gfortran is now at version 15.2 ???
    https://gcc.gnu.org/

    Version 12 of Gfortran was no longer supported at version 12.5 ???
    https://gcc.gnu.org/gcc-12/

    Lynn

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Louis Krupp@lkrupp@invalid.pssw.com.invalid to comp.lang.fortran on Fri Aug 15 02:09:23 2025
    From Newsgroup: comp.lang.fortran

    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 search for the fraction intrinsic turned up this:

    https://stackoverflow.com/questions/27686204/wrong-output-of-the-intrinsic-function-fraction-in-fortran

    Louis
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Thomas Koenig@tkoenig@netcologne.de to comp.lang.fortran on Fri Aug 15 16:16:42 2025
    From Newsgroup: comp.lang.fortran

    Louis Krupp <lkrupp@invalid.pssw.com.invalid> schrieb:
    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.

    For

    print *,FRACTION(3.75)
    print *,FRACTION(2.5)
    end program

    I get the same result for gfortran 12, 13, 14, 15 and current trunk:

    0.937500000
    0.625000000

    So, please provide a failing test case that actually compiles.
    --
    This USENET posting was made without artificial intelligence,
    artificial impertinence, artificial arrogance, artificial stupidity,
    artificial flavorings or artificial colorants.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Lawrence =?iso-8859-13?q?D=FFOliveiro?=@ldo@nz.invalid to comp.lang.fortran on Fri Aug 15 22:24:52 2025
    From Newsgroup: comp.lang.fortran

    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?
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Steven G. Kargl@sgk@REMOVEtroutmask.apl.washington.edu to comp.lang.fortran on Sun Aug 17 00:02:35 2025
    From Newsgroup: comp.lang.fortran

    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).
    --
    steve
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Gary Scott@garylscott@sbcglobal.net to comp.lang.fortran on Sat Aug 16 20:40:13 2025
    From Newsgroup: comp.lang.fortran

    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:

    "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. This is often denoted by curly
    braces {x} or frac(x).
    In more detail:
    Real Number Representation:
    Every real number can be expressed as the sum of an integer and a
    fractional part. For example, 3.14 can be written as 3 + 0.14, where 3
    is the integer part and 0.14 is the fractional part.
    Fractional Part Function:
    The fractional part function, denoted by {x}, is defined as the
    difference between the real number x and its integer part.
    Formula:
    {x} = x - ⌊x⌋, where ⌊x⌋ is the floor function (the greatest integer less than or equal to x).
    Example:
    If x = 3.14, then {x} = 3.14 - ⌊3.14⌋ = 3.14 - 3 = 0.14.
    Range:
    The fractional part of a number is always between 0 and 1 (inclusive of
    0, exclusive of 1). For integers, the fractional part is 0. "

    So, they would expect fraction(3.0) to be 0, not 0.75.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Steven G. Kargl@sgk@REMOVEtroutmask.apl.washington.edu to comp.lang.fortran on Sun Aug 17 03:18:03 2025
    From Newsgroup: comp.lang.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
    --
    steve
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Lawrence =?iso-8859-13?q?D=FFOliveiro?=@ldo@nz.invalid to comp.lang.fortran on Sun Aug 17 07:15:43 2025
    From Newsgroup: comp.lang.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.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Gary Scott@garylscott@sbcglobal.net to comp.lang.fortran on Sun Aug 17 09:11:52 2025
    From Newsgroup: comp.lang.fortran

    On 8/17/2025 2:15 AM, Lawrence D’Oliveiro wrote:
    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.

    Yes, which is abhorrent.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Steven G. Kargl@sgk@REMOVEtroutmask.apl.washington.edu to comp.lang.fortran on Sun Aug 17 22:30:15 2025
    From Newsgroup: comp.lang.fortran

    On Sun, 17 Aug 2025 07:15:43 +0000, Lawrence D’Oliveiro wrote:

    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.


    Most modern languages likely expect a user to do
    something like 'x = x - (int)x;' or 'x = x - int(x)'
    or some variation thereof if a user wants the decimal
    fraction.

    Let's see. Hmmm, C23 offer 'y = frexp(x,&n)', which
    returns the fractional part of the floating point
    representation. That would be 'x = y * 2**n' with y
    in [0.5,1) for a binary foating point number. Fortran
    2023 happens to have 'y = fraction(x)' with y in [0.5,1)
    for binary FP and does not have a side effect.

    C23 has 'y = frexpd64(x,&n)' but I doubt that the user
    uses '_Decimal64 x'. Not to mention, that frexpd64()
    returns 'x = y * 10**n' with y in [1/10,1), which is not
    the decimal fractional part that one gets with
    'x = x - (int)x;' Fortran 2023 allows a processor to
    support a base-10 REAL, and because 'fraction()' is a
    generic subprogram 'y = fraction(x)' would return
    y in [1/10,1) for a base-10 REAL x.

    Perhaps, you're referring to C23's modf(x, &y) function.
    But, C23's modf() does something entirely different than
    Fortran 2023 mod() (and modulo()) function. If Fortran
    had something like modf(), it would be a subroutine to
    avoid a function with a side effect.

    Finally, if x is an array, in Fortran one can do
    'x = x - (int)x'. C23 would be
    'for (int i = 0; i < imax; i++) x[i] = x[i] - (int)x[i];'
    One of these is a bit more elegant.
    --
    steve


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Kay Diederichs@kay.diederichs@gmx.net to comp.lang.fortran on Tue Aug 19 12:15:37 2025
    From Newsgroup: comp.lang.fortran

    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 .

    Hope this helps,
    Kay


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Steven G. Kargl@sgk@REMOVEtroutmask.apl.washington.edu to comp.lang.fortran on Tue Aug 19 15:29:34 2025
    From Newsgroup: comp.lang.fortran

    On Tue, 19 Aug 2025 12:15:37 +0200, Kay Diederichs wrote:

    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 .

    Looks like you've found a documentation bug. The Fortran standard
    actually has

    Result Value. The result has the value $X \times b^{-e}$,
    where $b$ and $e$ are as defined in 16.4 for the representation
    of X in the extended real model for the kind of X. ...

    where I have marked-up the sentence in LaTeX. IOW, when the gfortran documentation was written, someone transcripted the equation
    $X \times b^{-e}$ into Fortran without realizing RADIX returns an
    integer.
    --
    steve


    --- Synchronet 3.21a-Linux NewsLink 1.2