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?
Asked
Active
Viewed 1.1k times
5
Brett Champion
- 20,779
- 2
- 64
- 121
Fierce82
- 543
- 1
- 4
- 14
2 Answers
4
Two things might help you here:
- Make sure all your input is in floating point (
Real) form, notIntegers orRational. You might find this tutorial helpful. I am willing to bet that the cases where you get a fraction are not really random. - If wrapping your output in
Nis 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 thanN.
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}]
Verbeia
- 34,233
- 9
- 109
- 224
-
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 – Fierce82 Aug 01 '12 at 13:59
-
1As long as 1 value is MachinePrecision the output will be that too, isn't it? – Sjoerd C. de Vries Aug 01 '12 at 14:53


Nat the very last stage before sending the output to Java. – Verbeia Aug 01 '12 at 14:062instead of2.or `2`` – Rojo Aug 01 '12 at 19:02