Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2014

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

Search the Archive

Re: Compiler output type

  • To: mathgroup at smc.vnet.net
  • Subject: [mg132724] Re: Compiler output type
  • From: Daniel <dosadchy at its.jnj.com>
  • Date: Mon, 12 May 2014 22:26:14 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-outx@smc.vnet.net
  • Delivered-to: mathgroup-newsendx@smc.vnet.net

This will do the trick:
collatz = Compile[{{n, _Integer}}, If[EvenQ[n], n/2, 3 n + 1]//Round]

I think that the problem is with n/2. Although the result is guaranteed to be integer, the compiler cannot analyze the code and know it, so the output is set to Real.

> Some compiled functions yield reals even if the
> e input and necessary output are integers.  How can I
> force the output to be an integer rather than a real
> in such cases?  To be clear, not all compiled program
> output produces reals; some compiled programs, given
> integer inputs, produce integer outputs.  But some do
> not.  An example is:
> 
> collatz = Compile[{{n, _Integer}}, If[EvenQ[n], n/2,
> 3 n + 1]]
> 
> It produces real rather than integer output, and I'd
> like to force integer output if possible.
> 
> 	Thanks
> 
> 	Best,
> 
> 	Harvey
>



  • Prev by Date: Re: Cirlce in 3D?
  • Next by Date: Re: Compiler output type
  • Previous by thread: Re: Compiler output type
  • Next by thread: Building a matrix