0
$\begingroup$

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

Your help

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!!!

$\endgroup$
  • $\begingroup$ What does your function F look like? $\endgroup$ – SEngstrom Oct 9 '14 at 19:47
  • $\begingroup$ Does it work if you wrap Rationalize[] around the arguments to your $F?$ $\endgroup$ – Igor Rivin Oct 9 '14 at 20:40
  • $\begingroup$ People trying to help you can only guess if you don't share the code in detail. Please help us help you by editing your question, by using proper formatting and please include the code you are using to define the function. Only good questions deserve great answers. By the way, welcome to Mathematica.SE, please consider taking the very instructive tour. $\endgroup$ – rhermans Oct 9 '14 at 21:31
  • $\begingroup$ 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. $\endgroup$ – DDSY Oct 10 '14 at 1:05
  • $\begingroup$ @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. $\endgroup$ – DDSY Oct 10 '14 at 1:07
1
$\begingroup$

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]} *)

Or add the rationalization definition

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

Table[f[x], {x, 0.5, 2, 0.25}]
(* {-Log[2], -Log[4/3], 0, Log[5/4], Log[3/2], Log[7/4], Log[2]} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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