Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: appending '.' in CForm

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55453] Re: appending '.' in CForm
  • From: Peter Pein <petsie at arcor.de>
  • Date: Thu, 24 Mar 2005 03:41:52 -0500 (EST)
  • References: <d1rgua$42h$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Matt Flax wrote:
> Hello,
> 
> I have an equation which symbolicly evals. to a 1 MB file when streamed 
> to file using CForm ... it is a very very long equation !
> 
> I am unsatisfied with the results of CForm, because it writes longs, 
> rather then writing doubles out ... for example :
>       314572800*Power(b,2)*Power(d,4)*dr*e*Power(f,6)*Power(t,10)*
> instead of :
>       314572800.0*Power(b,2)*Power(d,4)*dr*e*Power(f,6)*Power(t,10)*
> 
> The issue is that the integer aproaches the limit of max. int. which is 
> listed in values.h (GNU compiler).
> 
> So ... how would I force CForm to print out integers as doubles ?
> 
> thanks
> Matt
Hi Matt,

 if you've got a polynomial equaltion, the following should work:

int2double[expr_] := expr //. (f:Plus | Times)[a___, i_Integer, b___] :>
    f[a, N[i, Ceiling[Log[10, Abs[i]]]], b]

let's try it:
test = Sum[(-1)^Random[Integer]*Random[Integer, {10^10, 10^12}]*x^j,
      {j, -1, 2}]
==>
-989314302571 - 259078934146/x + 755768626052*x + 10188600055*x^2

CForm[int2double[test]]
==>
-9.89314302571e11 - 2.59078934146e11/x + 7.55768626052e11*x +
1.0188600055e10*Power(x,2)

seems OK.

-- 
Peter Pein
Berlin


  • Prev by Date: Re: ReplaceAll / sequential replacements versus joint replacement
  • Next by Date: Re: Newbie Question About An Error Message
  • Previous by thread: Re: appending '.' in CForm
  • Next by thread: Newbie Question About An Error Message