FortranForm of Integers and Reals
- To: mathgroup at smc.vnet.net
- Subject: [mg60996] FortranForm of Integers and Reals
- From: Robert Schoefbeck <schoefbeck at hep.itp.tuwien.ac.at>
- Date: Fri, 7 Oct 2005 03:37:41 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Hello all,
I have a stupid problem when I try to export Fortran Code from
Mathematica into some .F file.
For example, I want to achieve that
Format[3xy, FormatType->FortranForm]
gives
3d0*x*y
and that
Format[3. xy, FormatType->FortranForm]
should give
3.d0*x*y
That is, I want to have Integers as xdn and reals as ydn where n is the
exponent of 10. First I tried
Unprotect[FortranForm];
FortranForm[int_Integer]:=DisplayForm[SequenceForm[int,d0]]
FortranForm[r_Real]:=
NumberForm[r,16,ExponentFunction\[Rule]Identity,
NumberFormat\[Rule](#1<>"d"<>#3&)]
Protect[FortranForm];
which gets me
FortranForm[1543254233.]
1.543254233d9
FortranForm[1]
1d0
which is JUST what I want, except that it doesnt work with ToString:
ToString[1543254233.,FormatType\[Rule]FortranForm]
1.543254233e9
ToString[1,FormatType\[Rule]FortranForm]
1
ToString[1.,FormatType\[Rule]FortranForm]
1.
The reason is that toString does not get the actual function FortranForm
but only the Identifier "FortranForm" and then proceeds as usual, i.e.
not as I want it to.
I have not succeded in redefining Format[int_, FortranForm] without
generating infinite recursions.
I do think that there should be an easy way out.
I'd very much appreciate any help.
Robert Schoefbeck