home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.fortran      Putting John Backus on a giant pedestal      5,127 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 4,971 of 5,127   
   Steven G. Kargl to All   
   Re: FRACTION() seems broken in gfortran    
   17 Aug 25 22:30:15   
   
   From: sgk@REMOVEtroutmask.apl.washington.edu   
      
   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   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca