# Using FortranForm for d0 notation? [duplicate]

When I use FortranForm, it changes 2*a^2 into 2*a**2 which is fine, but if I want the expression in a form such as,

2.d0*a**2


which represents an integer to be used with 16 digits, how can I make sure the output adheres to the correct syntax?

• actually, the duplicate is newer... Sep 17 '15 at 11:21

here is a stab/start at it.. (at least I hope to clarify what I think you are asking..)

 Clear[fort, fortranreal]
fort[x_Real ] := fortranreal[x]
fort[x_] := x
StringReplace[ ToString@MapAll[fort,  FortranForm[# ]] ,
Shortest[ "fortranreal(" ~~ s : __ ~~ ")" ] :> s <> "_db" ] & /@ {
2 a^2 ,
2. a^2,
2. 10^-20 a^3,
2. 10^51 a[3]^3.4 }

      2*a**2
a**2*2._db
a**3*2.e-20_db
a(3)**3.4_db*2.e51_db


Note I'm using the modern f90 syntax for assigning precision to literals, db here would be declared in the fortran as:

 integer,parameter::db=8


If you are using f77, the time to upgrade was in the last millennium.

Note also in most cases it is superfluous to specify precision to a real value that is an integer, since your fortran compiler will automatically upcast.

## old school form

 Clear[fort, fortranreal]
fort[x_Real ] := fortranreal[x]
fort[x_] := x
StringReplace[ ToString@MapAll[fort,  FortranForm[# ]] ,
{ Shortest[
"fortranreal(" ~~ s1 : NumberString ~~ "e" ~~
s2 : NumberString ~~ ")" ] :> s1 <> "D" <> s2,
Shortest[ "fortranreal(" ~~ s1 : NumberString ~~ ")" ] :> s1 <> "D0"} ] &
/@ {
2 a^2 ,
2. a^2,
2. 10^-20 a^3,
2. 10^51 a[3]^3.4  }

    2*a**2
a**2*2.D0
a**3*2.D-20
a(3)**3.4D0*2.D51

• iN YOUR CASE OUTPUT SHOULD BE 2.do a2 , 2.do* a2, 2.do* 10**-20* a3, 2.do* 1051* a(3)**3.4 but you shown it different. anyways i have made my point what i actually want in my output. you must have understood now. thanks Nov 10 '14 at 20:39
• I understood you asked for the antiquated d0 (not "do") notation. That's harder to do with no advantage over the modern form. ( I cant see at all how a^2 becomes a2 though.?) Nov 10 '14 at 21:20
• actually when i typed a^2 into a**2 , it just took by mistake a2. Apologise for this Nov 10 '14 at 22:05
• I see, you need to escape some of the "*" (I edited to fix that..) Nov 10 '14 at 22:18
• this method still not works for me. Nov 12 '14 at 12:59