0

I am just doing my first steps with Fortran, and the initial joy of having 128-bit integers has vanished...

program multiples_of_3_and_5

    implicit none
    
    ! n is the number to test whether it is divisible by 3 or 5
    !
    ! kind = 8 defines a 64-bit integer (8 bytes = 64 bits)
    integer(kind=8) :: n
    
    ! n stores the sum of all n which are divisible by either 3 or 5
    integer(kind=8) :: s
    
    do n = 1, 1000-1
        print *, "n = ", n
        if (mod(n, 3) == 0 .or. mod(n, 5) == 0) then
        
            print *, "n is divisible by either 3 or 5"
            s = s + n
        
        end if
        print *, "s = ", s
    end do
    
    print *,"The result is: ", s
    
end program multiples_of_3_and_5

When I use kind=4 or 16 or 32, it doesn't give the correct result anymore - even when no integer overflow occurs.

Edit: Example output (with kind=16)

 n is divisible by either 3 or 5
 s =   -23552265132012064450548819805997922555
 n =                                       996
 n is divisible by either 3 or 5
 s =   -23552265132012064450548819805997921559
 n =                                       997
 s =   -23552265132012064450548819805997921559
 n =                                       998
 s =   -23552265132012064450548819805997921559
 n =                                       999
 n is divisible by either 3 or 5
 s =   -23552265132012064450548819805997920560
 The result is:   -23552265132012064450548819805997920560
user258532
  • 511
  • 5
  • 11
  • 1
    I must have missed it! Did you initialise s? – lastchance Dec 22 '22 at 12:14
  • 1
    Also, kind=x is very compiler-dependent. Use the more specific alternatives in the standard module iso_fortran_env. – lastchance Dec 22 '22 at 12:15
  • 1
    The uninited s is probably the problem, but in future please explain exactly *how* your code doesn't work; not all of us are psychic. Also I second the comment about kinds. Kind=8 is most definitely *not* guaranteed to give you a 64 bit integer, and the is not even guaranteed to be supported by your compiler. For instance the NAG compiler doesn't support it (by default) – Ian Bush Dec 22 '22 at 13:05
  • @IanBush I just updated the question. I use the gfortran compiler. Now with initializing s = 0 it works, even with kind=16. I find it weird that Fortran doesn't complain about un-initialized variables, but does not assume s = 0 either. – user258532 Dec 22 '22 at 13:52
  • 1
    Fortran compilers often can, _if asked_, complain about the use of undefined variables, but fundamentally it is the programme's responsibility to write correct Fortran code. In this case, not defining `s` before referencing it is a clear violation of that requirement. – francescalus Dec 22 '22 at 13:56
  • Voted to reopen as ilf the linked question is a dupe for this one, it is a dupe for the vast majority of questions featuring buggy code here, and so they could all be dealt as with this one. Very easy, not very useful IMO. Something more specific to uninited cars would be better – Ian Bush Dec 22 '22 at 14:50
  • Try `-Wall` in gfortran or `-warn` in Intel when compiling. Also try `-fsanitize=undefined` if using gfortran and run the code. Or try valgrind. Be aware that kind=8 is not guaranteed to be 8 bytes for all compilers not it is guaranteed to even exist. – Vladimir F Героям слава Dec 22 '22 at 15:40

0 Answers0