Manual Reference Pages  - sign (3m_overload)

NAME

sign(3f) - [M_overload::INTRINSIC] When sign(3f) is given a single value sign(value), call sign(1,value). (LICENSE:PD)

CONTENTS

Synopsis
Description
Options
Returns
Examples
Author
License

SYNOPSIS

pure elemental class(*) function sign([magnitude],copysign)

DESCRIPTION

This is an overload of the SIGN(3f) intrinsic which assumes the magnitude is 1 if only one argument is supplied.

SIGN(3f) returns a value with the magnitude of MAGNITUDE but with the
sign of COPYSIGN.
  All three must be of the same type, which may be INTEGER or REAL.

OPTIONS

magnitude
  the return value will have the same magnitude as this value Optional. A 1 is assumed if not present.
copysign
  the return value will have the same sign as this value

RETURNS

The result is a value with the magnitude of A and the sign of B

EXAMPLES

Sample usage:

   program demo_sign
   use, intrinsic :: iso_fortran_env, only : &
   & integer_kinds, int8, int16, int32, int64
   use, intrinsic :: iso_fortran_env, only : &
   & real32, real64, real128
   ! overload can take a single argument
   use M_overload, only: sign, lt, le, eq, ne, gt, ge
   implicit none
   ! basics
   write(*,*) sign(1234), sign(-1234), sign(huge(0.0)), sign(-huge(0.0))
   ! any type of integer or real
   write(*,*) merge(’sign works’,’sign fails’, sign(10_int8).eq.1         &
   & .and. sign(-10_int8).eq.-1        )
   write(*,*) merge(’sign works’,’sign fails’, sign(10_int16).eq.1        &
   & .and. sign(-10_int16).eq.-1       )
   write(*,*) merge(’sign works’,’sign fails’, sign(10_int32).eq.1        &
   & .and. sign(-10_int32).eq.-1       )
   write(*,*) merge(’sign works’,’sign fails’, sign(10_int64).eq.1        &
   & .and. sign(-10_int64).eq.-1       )
   write(*,*) merge(’sign works’,’sign fails’, sign(10.0_real32).eq.1.0   &
   & .and. sign(-10.0_real32).eq.-1.0  )
   write(*,*) merge(’sign works’,’sign fails’, sign(10.0_real64).eq.1.0d0 &
   & .and. sign(-10.0_real64).eq.-1.0d0 )
   write(*,*) merge(’sign works’,’sign fails’, sign(10.0_real128).eq.1.0  &
   & .and. sign(-10.0_real128).eq.-1.0 )
   !
   !write (*, *) sign(10 < 20)
   !
   !if (sum(sign([1 > 2, 3 == 4, 10 < 5, 100 > 50])) > 2) then
   !   write (*, *) ’two or more are not true’
   !endif
   !
   end program demo_sign

Results:

    >  sign works
    >  sign works
    >  sign works
    >  sign works
    >  sign works
    >  sign works
    >  sign works
    >           0
    >  two or more are not true

AUTHOR

John S. Urban

LICENSE

Public Domain


Nemo Release 3.1 sign (3m_overload) July 24, 2024
Generated by manServer 1.08 from a60bfa07-d236-47a7-a636-4248f6970592 using man macros.