Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple question for implementing a c++ operation in Fortran

Status
Not open for further replies.

ya0037

Programmer
Oct 7, 2010
30
DE
Hi,

Is there any one familiar with c++ who can help me implement this one in Fortran

Code:
static const unsigned int d1=0xDEADBEEF ^ (0x1fc4ce47*(d^(d>>13)));

Is the below one is the correct implementation:
Code:
Integer :: d1, d
Integer, Parameter :: cns1=Z'DEADBEEF'
Integer, Parameter :: cns2=Z'1fc4ce47'

d1=Ieor(cns1, cns2*Ieor(d, Ishft(d, -13)))

The above code gives me error the precision is not enough, but why?
If I change it to Integer(Kind = 16) it will work, but I do not understand in the c++ version they have used a simple 4 byte integer and that is enough, but in FORTRAN I have to use 16 bytes ????????????

Please some one give me a hint or st.

Regards,


 
First of all, what is the value of "d" ? I don't see its declaration.

Normally all is OK if "d" is also declared INTEGER.


François Jacq
 
yes d is unsigned int

But my program in FORTRAN gives error, so it is not OK:

Code:
Error: Arithmetic overflow converting INTEGER(16) to INTEGER(4)

Please tell me what should I do, if I want to convert it to higher precision I will come with a lot of problem, should I do that is there any solution?

Thank you very much.
 
This is the whole error.
Please tell me what should I do?

Code:
Integer, Parameter :: cns1=Z'DEADBEEF'
                            1
Error: Arithmetic overflow converting INTEGER(16) to INTEGER(4) at (1). This check can be disabled with the option -fno-range-check

regards,
 
I checked your program with ifort. It works correctly.
But effectively, gfortran issues an error message :

Code:
t47.f90:3.27:

Integer, Parameter :: cns1=Z'DEADBEEF'
                           1
Error: Arithmetic overflow converting INTEGER(16) to INTEGER(4) at (1). This check can be disabled with the option -fno-range-check
t47.f90:6.8:

d1=Ieor(cns1, cns2*Ieor(d, Ishft(d, -13)))
        1
Error: 'i' argument of 'ieor' intrinsic at (1) must be INTEGER

Of course, only the first message has to be examined (the second one is just a consequence of the first one). This message is due to the fact that Z'DEADBEEF', which is a positive binary integer, cannot be represented by a positive integer when an INTEGER(4) is used.

On the C side, you use an unsigned int. Such unsigned integer does not exist in Fortran.

You can follow the advice given by the compiler : add the option -fno-range-check. Your program compiles perfectly with this option.

Else, you have the possibility to modify slightly your program to make it standard conforming :

Code:
program test

integer,parameter :: long=selected_int_kind(10)
Integer :: d1, d=5
Integer(long), Parameter :: cns1=Z'DEADBEEF'
Integer(long), Parameter :: cns2=Z'1fc4ce47'

d1=Ieor(cns1, cns2*Ieor(d, Ishft(d, -13)))
write(*,*) d1
end program

As d1 is positive here, only the intermediate constants have to be declared "long".




François Jacq
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top