forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Floor hangs with 64 bit values

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
Ryan



Joined: 25 Feb 2016
Posts: 73

PostPosted: Sun Jul 21, 2019 2:04 pm    Post subject: Floor hangs with 64 bit values Reply with quote

I may be doing something daft, I'm trying to generate a 64 bit random number. The following hangs when floor is called.

Quote:

REAL :: r, z
integer(kind=3):: j

CALL random_seed()
CALL RANDOM_NUMBER(r)
print *, "Random is ", r
print *, "huge is ", Huge(j)
z = Huge(j) *r
print *, "z is ", z
! Next line hangs.
j = floor(Huge(j)*r)
print *, "j is ", j


I can make this work for a 32 bit integer by changing to;

Quote:

integer(kind=3):: j


What dumbness have I emitted? Also, how should I declare the integer j to avoid the compiler warning if I want a 32 or 64 bit integer?

For example, 32 bit shows;

Quote:

COMMENT - Specifying the kind of the type INTEGER with a constant is non-portable - 'SELECTED_INT_KIND(9)' would be
better


Ryan
Back to top
View user's profile Send private message
davidb



Joined: 17 Jul 2009
Posts: 557
Location: UK

PostPosted: Sun Jul 21, 2019 7:07 pm    Post subject: Reply with quote

It looks like overflow is occurring with the argument to floor.

You have to be careful that the argument to floor is not outside the range of the largest default integer or the largest integer of a kind specified as an optional argument:

Code:

   j = floor(huge(j)*r, kind(j))


Just because you are multiplying the largest integer (huge(1)) by a value r that is smaller than 1 doesn't mean that overflow won't occur when r is close to 1 (due to floating point rounding).

If you really need 63 bits you could generate two 32 bit values and concatenate them together (using IOR).

Its better not to use RANDOM_NUMBER for this but to use your own random number generator to generate the integers you want. This way you can make your code portable to different compilers and avoid the problems above.

See for examples

http://www.firstpr.com.au/dsp/rand31/p1192-park.pdf
https://create.stephan-brumme.com/mersenne-twister/
_________________
Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 5959
Location: Salford, UK

PostPosted: Tue Jul 23, 2019 3:49 pm    Post subject: Reply with quote

Ryan

I think that there is something here that we need to fix for 64 bits.
If you start with integer(4)::j then it looks like floor(z, 4) is missing.

I have made a note of this.
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 5959
Location: Salford, UK

PostPosted: Wed Jul 24, 2019 11:21 am    Post subject: Reply with quote

This has now been fixed for 64 bit code. It applies to FLOOR and CEILING and the fix will be in the next release of FTN95 and clearwin64.dll.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group