Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

split of floating point number into mantissa and exponent ???

72 views
Skip to first unread message

Richard Maine

unread,
Dec 17, 1998, 3:00:00 AM12/17/98
to
"Jens Becher" <bec...@atlantis.wh2.tu-dresden.de> writes:

> I am a Fortran newby and have a little problem.
> How can I split a floating point number into
> mantissa and exponent in Fortran ?
> (e.g. 1.321345 *10**(-6) --> 1.321345 ; -6 )
> Is this possible in Fortran ?
> In Mathematica I write MantissaExponent[1.321345 *10**(-6) ]
> and get {1.321345, -6}.

Are you picky that this be a base 10 representation? In Fortran 90
(or 95) there are the EXPONENT and FRACTION intrinsics. They return
the exponent and mantissa, but it will be in the base used for
internal representation of floatting point - most often binary,
but hex is at least a possibility (I don't know whether any of the radix
16 machines have f90 compilers). Fortran 77 doesn't have anything
like that.

If you want a base 10 representation, you'll need to do one yourself,
probably using the log10 intrinsic. Shouldn't be hard, but I'll
let someone else figure it out (because if I try to throw it
together off the top of my head, I'll probably mess up and get
off by 1 in the exponent or something like that).

--
Richard Maine
ma...@altair.dfrc.nasa.gov

Jerry A Green

unread,
Dec 17, 1998, 3:00:00 AM12/17/98
to Jens Becher
Jens Becher wrote:
>
> Hello,

> I am a Fortran newby and have a little problem.
> How can I split a floating point number into
> mantissa and exponent in Fortran ?
> (e.g. 1.321345 *10**(-6) --> 1.321345 ; -6 )
> Is this possible in Fortran ?
> In Mathematica I write MantissaExponent[1.321345 *10**(-6) ]
> and get {1.321345, -6}.
> Hope someone can help.
> Thanks.
> Bye
>
> ----------------------------------------------------------------------------
> Jens Becher (bec...@theory.phy.tu-dresden.de)
> TU-Dresden (Bldg. BZW, Room 131), Zellescher Weg 17, D-01069 Dresden
> Voice: +49 351 463-3920 (Private: +49 351 472 446 3)
> ICQ: #18174425 PGP-Key-ID: 0xE2A3E769

Here is a method which will work in Fortran 77:

PROGRAM MAIN

IMPLICIT NONE

CHARACTER*14 STRING
CHARACTER*1 ANS
INTEGER INDX
REAL X
REAL RMAN
INTEGER IEXP

10 WRITE (*,*) 'Enter real number X:'
READ (*,*,ERR=10) X

* Desired form of output to character string:
* 12345678901234 length of format = 14
* +d.ddddddE+ddd

* Test output format:
WRITE (*,'(1P,E14.6E3)') X

* Write X to character string then output string segments:
WRITE (STRING,'(1P,E14.6E3)',ERR=10) X
* Find position of 'E' in the string:
INDX = INDEX (STRING,'E')
WRITE (*,*) '{', STRING(1:INDX-1), ',', STRING(INDX+1:14),'}'

* Write X to character string then extract numerical components:
WRITE (STRING,'(1P,E14.6E3)',ERR=10) X
* Find position of 'E' in the string:
INDX = INDEX (STRING,'E')
READ (STRING(1:INDX-1),'(F13.0)') RMAN
READ (STRING(INDX+1:14),'(I10)') IEXP
WRITE (*,*) '{', RMAN, ',',IEXP,'}'

20 WRITE (*,*) 'Do another (Y/N)?'
READ (*,'(A)') ANS
IF ( ANS .EQ. 'Y' .OR. ANS .EQ. 'y' ) THEN
GOTO 10
ELSEIF ( ANS .EQ. 'N' .OR. ANS .EQ. 'n' ) THEN
CONTINUE
ELSE
GOTO 20
ENDIF

STOP
END

I hope that this helps.

Jerry . . .

--
Jerry A Green mailto:j...@cs-software.com
Custom Solutions http://www.cs-software.com/
209 Bayberry Run
Summerville, SC 29485-8778 Your source for discounted
Voice: (843) 871 9081 Fortran compilers and
Fax: (843) 873 8626 related software

Jens Becher

unread,
Dec 18, 1998, 3:00:00 AM12/18/98
to

Steven G. Kargl

unread,
Dec 18, 1998, 3:00:00 AM12/18/98
to
In article <uevhjab...@altair.dfrc.nasa.gov>,
Richard Maine <ma...@altair.dfrc.nasa.gov> writes:

> "Jens Becher" <bec...@atlantis.wh2.tu-dresden.de> writes:
>
>> I am a Fortran newby and have a little problem.
>> How can I split a floating point number into
>> mantissa and exponent in Fortran ?
>> (e.g. 1.321345 *10**(-6) --> 1.321345 ; -6 )
>> Is this possible in Fortran ?
>> In Mathematica I write MantissaExponent[1.321345 *10**(-6) ]
>> and get {1.321345, -6}.
>
> Are you picky that this be a base 10 representation? In Fortran 90
> (or 95) there are the EXPONENT and FRACTION intrinsics. They return
> the exponent and mantissa, but it will be in the base used for
> internal representation of floatting point - most often binary,
> but hex is at least a possibility (I don't know whether any of the radix
> 16 machines have f90 compilers). Fortran 77 doesn't have anything
> like that.
>
> If you want a base 10 representation, you'll need to do one yourself,
> probably using the log10 intrinsic. Shouldn't be hard, but I'll
> let someone else figure it out (because if I try to throw it
> together off the top of my head, I'll probably mess up and get
> off by 1 in the exponent or something like that).
>

c
c Mantissa should be between -1 and 1
c
subroutine manexp(x, m, e)
integer e
real xx, x, m

if (x .lt. 0.) then
xx = -x
e = int(log10(xx))
if (e .gt. 0) e = e + 1
m = - xx * 1.e1 ** (-e)
else if (x .gt. 0.) then
e = int(log10(x))
if (e .gt. 0) e = e + 1
m = x * 1.e1 ** (-e)
else
e = 0.
m = 0.
endif
end


--
Steve

robin

unread,
Dec 20, 1998, 3:00:00 AM12/20/98
to
ka...@troutmask.apl.washington.edu (Steven G. Kargl) writes: > In article <uevhjab...@altair.dfrc.nasa.gov>,

> Richard Maine <ma...@altair.dfrc.nasa.gov> writes:
> > "Jens Becher" <bec...@atlantis.wh2.tu-dresden.de> writes:
> >
> >> I am a Fortran newby and have a little problem.
> >> How can I split a floating point number into
> >> mantissa and exponent in Fortran ?
> >> (e.g. 1.321345 *10**(-6) --> 1.321345 ; -6 )
> >> Is this possible in Fortran ?
> >> In Mathematica I write MantissaExponent[1.321345 *10**(-6) ]
> >> and get {1.321345, -6}.
> >
> > Are you picky that this be a base 10 representation? In Fortran 90
> > (or 95) there are the EXPONENT and FRACTION intrinsics. They return
> > the exponent and mantissa, but it will be in the base used for
> > internal representation of floatting point - most often binary,
> > but hex is at least a possibility (I don't know whether any of the radix
> > 16 machines have f90 compilers). Fortran 77 doesn't have anything
> > like that.
> >
> > If you want a base 10 representation, you'll need to do one yourself,
> > probably using the log10 intrinsic.

To get decimal representation, try WRITE (String, '(E15.5)' ) X
where String is CHARACTER (len=15).
You get the mantissa and the exponent, which can be re-processed in a
variety of ways.

> Shouldn't be hard, but I'll
> > let someone else figure it out (because if I try to throw it
> > together off the top of my head, I'll probably mess up and get
> > off by 1 in the exponent or something like that).

> Steve


0 new messages