Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a mathematica file with an algorithm that feeds another program with data. my problem is that some times (randomly) the output is in fraction instead of decimal. The other program (java) does not read fractions, so I have to feed it with decimal numbers. One solution is to use the N[.] function before it output, but this is really painful procedure. Is there any setting in mathematica forcing it to return as output only decimals?

share|improve this question
3  
"...but this is really painful procedure..." - why? –  J. M. Aug 1 '12 at 13:28
1  
By the way, welcome to Mathematica.SE! Please consider registering your account, so that any votes you might get for this question are added to those received for future questions. As you gain reputation points, you will be able to do more things like participate in the chat room. –  Verbeia Aug 1 '12 at 13:42
    
I have several algorithms, pretty big ones, and I was seeking for a general way to overcome my problem before getting inside the code and start messing with it. Thanx for the hint, i will do register. –  bill zag Aug 1 '12 at 13:54
    
You only need to use N at the very last stage before sending the output to Java. –  Verbeia Aug 1 '12 at 14:06
1  
Take @Verbeia 's advice on using real inputs seriously, specially if you have a long heavy weight algorithm. The difference in space and time it can make is enormous, and only generated by inputing, e.g, 2 instead of 2. or 2` –  Rojo Aug 1 '12 at 19:02
add comment

2 Answers

You can use $Post:

enter image description here

If you set $Post to N, N is applied to the output of every evaluation:

 Cos[Pi/4]

enter image description here

$Post = N;
Cos[Pi/4]
(* 0.707107  *)

To restore $Post to its default value, use

 $Post =.
share|improve this answer
add comment

Two things might help you here:

  1. Make sure all your input is in floating point (Real) form, not Integers or Rational. You might find this tutorial helpful. I am willing to bet that the cases where you get a fraction are not really random.
  2. If wrapping your output in N is unacceptable to you for some reason, you could instead use a rule once you have your output: data /. x_Rational :> N@x. This will almost certainly be slower than N.

For example:

{1/2, 256/225, 0.6} /. x_Rational :> N@x

(* output is {0.5, 1.13778, 0.6}*)

N is Listable, so the following also works:

N[{1/2, 256/225, 0.6}]
share|improve this answer
    
first of all thank you for your reply! I will check the tutorial u sent me. I was overexaggerating when i said randomly, but frankly, i give an initial input of 5 variables in my algorithm, and i get hundreds of outputs, and the percentage of getting fractions instead of decimal numbers is not greater than 1% based on my hundreds and hundreds of tests. nevertheless, once i get fraction, the second program is useless (java). The second solution you suggest seems to be similar to the N[.] function, with respect to the editing i have to do in my code. i was wondering if there was a setting hidden –  bill zag Aug 1 '12 at 13:59
    
As long as 1 value is MachinePrecision the output will be that too, isn't it? –  Sjoerd C. de Vries Aug 1 '12 at 14:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.