Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28701 Discussions

Is is safe to assign real*8 to real*4 after bound check?

rookie2010
Beginner
800 Views

Hi there,

We're using 64-bit  ifort  on linux and its version is  11.1 056.

I have to assign the value of a real*8 variable var_r8 to another real*4 variable var_r4.

If var_r8 is checked to be within upper/lower bounds of real*4, is it save to use
var_r4 = var_r8

In case var_r8 exceeds the smallest number which can be handled by real*4, var_r4 can be set as 0.0.

 

Your opinion is very much appreciated.

0 Kudos
5 Replies
TimP
Honored Contributor III
800 Views

If your only criterion of safety is that the assignment doesn't overflow, you are OK under the condition abs(x) < huge(1_4)

0 Kudos
rookie2010
Beginner
800 Views

Tim,

Thanks a lot for the info.  Function "huge" is the one to implement.   Do you know the corresponding function to show the smallest real*4?   I surfed the Internet but only found the compiler flags which handle floating point underflow.  However, I need to assign real*8 to real*4 and those flags are not too useful.

Thank you!

 

0 Kudos
TimP
Honored Contributor III
800 Views

You implied you weren't concerned about underflow.  Partial or full underflow will set in for abs(x) < tiny(x).

If you are talking about compile options for abrupt underflow (such as ifort default -ftz) those simply assure that there is no partial underflow, so that real(x, kind(1.) ) always gives 0. for abs(x) < tiny(x).

-ftz has an effect only when used to compile the main program.  If you wish to switch underflow mode at run time, under

USE ieee_arithmetic

...

logical gradual_underflow

....

gradual_underflow= .....

call ieee_set_underflow_mode(gradual_underflow)

The only reason for introduction of these complications was that CPUs prior to Core I7-2 "Sandy Bridge" exhibited poor performance when encountering gradual underflow.  It's not clear that these facilitate whatever reasons you may have for reducing double to single precision.

0 Kudos
JVanB
Valued Contributor II
800 Views

HUGE and TINY are Fortran intrinsics; no need to implement them yourself, just look at ifort's excellent documentation. Also the masked assignment with the WHERE statement and construct may be consistent with your needs.

0 Kudos
rookie2010
Beginner
800 Views

Tim and Repeat Offender,

Thanks a lot for the assistance.  I've got all I need.

 

 

0 Kudos
Reply