# How to work with decimals (e.g. 0.95) instead of fractions (95/100) and get the same result

In my code I have a function, F, that depends on several parameters, say a,b,c: F(a,b,c). I need to give a grid of values to a,b and c to obtain the value of F.

My grid of values are decimal numbers. For example, a goes from 0.99 to -0.99 at intervals of 0.1. Same for b and c.

Problem: when I use say a=0.95 I obtain a different result than for a=95/100.

Here is a specific example where it matters:

F>= 0 always (mathematically this is true)

F(95/100,85/100,75/100)=0 (this I can easily prove mathematically)

BUT when I use decimal numbers instead I get

F(0.95,0.85,0.75)<0

More insights

I don't know if this is relevant, but one case where I detected this issue is when I use the Log function:

I have that F(0.2,0.4,0.5)=1. while F(2/10,4/10,5/10)=1 Thus, when I use decimals I get Log(1.)<0 but Log(1)=0 for when I use fractions

I've been reading a lot on mathematica precision and the difference between having 1. vs 1

What I'm asking from you is a suggestion on how to keep using decimals and getting the proper answer.

Thanks!!!

• What does your function F look like? – SEngstrom Oct 9 '14 at 19:47
• Does it work if you wrap Rationalize[] around the arguments to your $F?$ – Igor Rivin Oct 9 '14 at 20:40
• I know, but there is reason why I didn't specify the function, it is not a simple one like f(x)=a+x. It is something that results after many lines of codes and manipulation of objects. Since my question was very specific, I thought it this case the specific functional form is not necessary. Any solution to the simple problem I described would work for my problem. – DDSY Oct 10 '14 at 1:05
• @Igor Thanks! I actually found the function Rationalize[] after this post, and so far is the best solution I have. I wonder if there is anything more general you can do to tell mathematica 0.9 is as good as 90/100. – DDSY Oct 10 '14 at 1:07

Let you have a function

f[x_] := Log[x]


You can use rational parameters in the Table (or Do, Range, etc.)

Table[f[x], {x, 1/2, 2, 1/4}]
(* {-Log[2], -Log[4/3], 0, Log[5/4], Log[3/2], Log[7/4], Log[2]} *)


f[x_?MachineNumberQ] := f[Rationalize[x]]