This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [gfortran] Implement irand(), rand(), and srand() for G77 compatibility


On Sun, May 30, 2004 at 06:21:28PM -0700, Steve Kargl wrote:
> > You're forgetting that real*4 has significantly less than 32 bits
> > precision,  so is unable to accurately represent integers of this
> > magnitude. After rounding the result will in the range [0-1]
> 
> Thanks for the wack with the clue stick.
> 
> The simple fix is
> 
>   return (GFC_REAL_4) ((double)(prefix(irand)(i) - 1) / (double)GFC_RAND_M1);

Except if you *really* don't want 1.0 as a result, this won't work either,
because the double->float conversion will round.  I'm pretty sure what you
want is truncated rounding, but I'm uncertain how to give you that 
portably.

Probably the easiest portable fix is 

	GFC_REAL_4 r = (prefix(irand)(i) - 1) / (GFC_REAL_4)GFC_RAND_M1;
	if (r == 1)
	  r = nextafterf (1, 0);
	return r;

mod whatever markup you use to select nextafter{,f,l} based on the 
underlying type of GFC_REAL_4.


r~


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]