One line question: How to make
1/2 instead of
0.5? (Thanks Nasser's comment!)
I've been using
CForm to convert Mathematica expressions to C codes for a long time. Although the built-in C strings are a little bit annoying (e.g.,
Sqrt is translated to "Sqrt(2)" instead of "sqrt(2)"), it works.
However, recently I found an even more annoying behavior which I cannot find a workaround so far. Consider converting the following simple piece extracted from a rather complicated expression
3/40007 + (2 test )/40007//CForm
0.00007498687729647311 + (2*test)/40007.
which is totally bizarre! What I expect is simply
3/40007. + (2*test)/40007.
which is trivial yet crucial to my following work. (To be clear, I don't mean the presence of the decimal point is crucial! Rather, I mean the form of numbers should no be changed.) Can anyone come up with a solution, at least to tell
CForm stop doing such a floating number conversion? Certainly I could manually rewrite this piece (or use
Together) like this
(3 + 2 test)/40007 // CForm
(3 + 2*test)/40007.
But given that I have tons of complicated expressions to be converted, it is super inefficient for sure. Besides there's a possibility that
test = I, and
3/40007 + (2 I)/40007 // Together returns itself.
PS. It is surprising that the discussions related to
CForm and other C utilities are so rare on this site! Am I missing something?