2
$\begingroup$

I am wondering if it is possible to get the correct numerical result without computing with a lot of precisions in expressions.

as a simple example suppose in the middle of some of my numerical code there is something like:

Log[1 +3 E^-10000] /E^-10000

The obvious fact is that the expression is just "3". But because the computation is numeric if I want to get the correct result I had to evaluate the expression with a precision of 10000! For example with N[#,10000]&. And then the result is something like:

2.99999999999999999999999999999999999999999999999999999999999999999999\
9999999999999999999999999999999999999999999999999999999999999999999999\
9999999999999999999999999999999999999999999999999999999999999999999999\
9999999999999999999999999999999999999999999999999999999999999999999999\
9999999999999999999999999999999999999999999999999999999999999999999999\
9999999999999999999999999999999999999999999999999999999999999999999999\
9999999999999999999999999999999999999999999999999999999999999999999999\
9999999999999999999999999999999999999999999999999999999999999999999999\
9999999999999999999999999999999999999999999999999999999999999999999999\
9999999999999999999999999999999999999999999999999999999999999999999999\
9999999999999999999999999999999999999999999999999999999999999999999999\
9999999999999999999999999999999999999999999999999999999999999999999999\
9999999999999999999999999999999999999999999999999999999999999999999999\
9999999999999999999999999999999999999999999999999999999999999999999999\
9999999999999999999999999999999999999999999999999999999999999999999999\
9999999999999999999999999999999999999999999999999999999999999999999999\
9999999999999999999999999999999999999999999999999999999999999999999999\
9999999999999999999999999999999999999999999999999999999999999999999999\
9999999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999

And I don't need that much precision and want to get the result much easier than computing each part of the expression (here the numerator and denominator to a high degree of precision 1000). I just want it to be about 3. looking at the expression Log[1 +3 E^-10000] /E^-10000, it is obvious that it is 3. But what Mathematica has to do to get this result is that it should evaluate both the numerator and denominator to a high degree of precision. Isn't it possible for Mathematica to do some symbolic numerical examination of expressions before evaluating them numerically? My problem is that in my problem (due to the appearance of such quantities) I did not know how much set the precision. and then also a there is a problem with the time needed for high-precision evaluation. Suppose the expression appear in a sum like this:

 Sum[Log[1 + 3 E^-n]/E^-n, {n, 0, 1000}] //N

Evaluating that without enough precision did not lead to the correct result. The sum after about 5 terms is ~ 3 for each term. They have a finite contribution to the sum. On the other hand, the first terms in the sums differ from 3.

I symbolic computation we know:

Series[Log[1 + 3 x]/x, {x, 0, 1}]
(*    3 - 9x/2 + O[x]^2    *)

But in the numeric case, is there possible to write a function that examines each part of an expression and threat numbers smaller than some \epsilon in a manner similar to the above symbolic result?

I hope, I have made my question clear. I would be grateful If anyone could help with that.

Thanks in advance.

$\endgroup$
13
  • 1
    $\begingroup$ Welcome to Mathematica StackExchange. I am slightly confused. It is actually an obvious fact that the result is not 3. Also, you say you want it to be "almost 3" – what does that mean? Perhaps it will help if you include more details about your actual problem (your whole code and the problem that you want to solve). $\endgroup$
    – Domen
    Feb 5 at 15:22
  • $\begingroup$ Welcome to Mathematica S.E. To start: 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, since the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) consider accepting the answer, if any, that solves your problem, by clicking checkmark sign, 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – bmf
    Feb 5 at 15:31
  • $\begingroup$ @Domen. I mean that my problem is not with the precision of the final result is not very important for my say 2.8, 2.9, 3.01, or 3.1. The problem is that if I don't evaluate the expression with a precision smaller than 10000 I will get 0 or Infinity at the output or just an error message. I am asking about getting approximate result without large precision. $\endgroup$ Feb 5 at 15:44
  • $\begingroup$ "approximate result without large precision" -- somehow I'm having trouble grasping what you're after. The difference between your example number and $3$ is around $5\times10^{-4343}$. If you want to approximate without rounding to $3$, you will need more than 4343 digits of precision. My best shot if you want machine precision: Block[{$MaxExtraPrecision = 5000}, Floor[Log[1 + 3 E^-10000]/E^-10000, 2 $MachineEpsilon]] Not sure how to automate beyond the example, though. $\endgroup$
    – Michael E2
    Feb 5 at 15:56
  • 2
    $\begingroup$ @ВалерийЗаподовников - It would have to be evaluated numerically to determine that. Mathematica doesn't arbitrarily evaluate exact values numerically (performance would be impacted). If you want it to look at the numeric values, use PossibleZeroQ[Log[1 + 3 E^-10000]/E^-10000 - 3] which evaluates to False $\endgroup$
    – Bob Hanlon
    Feb 5 at 17:32

2 Answers 2

2
$\begingroup$

Try a series expansion:

Series[Log[1 + 3 x]/x, {x, 0, 1}]
(*    3 - 9x/2 + O[x]^2    *)

Normal[%] /. x -> E^-10000
(*    3 - 9/(2 E^10000)    *)
$\endgroup$
5
  • $\begingroup$ Thanks, @Roman. In fact, in my actual computation, I can not track the form. This was an example that I realized appears. I exactly mean that some expressions have obvious results when seen symbolically but are hard to get the correct results numerically. $\endgroup$ Feb 5 at 16:09
  • $\begingroup$ Is it a bug that it cannot see those are equal? FullSimplify[3 - 9/(2 E^10000) == Log[1 + 3 E^-10000] /E^-10000] returns unevaluated after 15 minutes. $\endgroup$ Feb 5 at 17:14
  • $\begingroup$ @ВалерийЗаподовников They are not equal. Please read the documentation of O or the Wikipedia. What's done here is a Taylor series expansion that is only valid for very small $x$ (i.e., in the limit $x\to0$). For $x=e^{-10000}$, which is very small, the result is pretty accurate, but not exact. $\endgroup$
    – Roman
    Feb 5 at 17:57
  • 1
    $\begingroup$ @ghadirjafari I suspect you are presenting an XY problem here. Why do such extremely small numbers appear in your formulas? Did you introduce them on purpose? Is there a way to make them disappear? Extremely small numbers like $e^{-10000}$ are unlikely to appear in formulas spontaneously; they are often the result of poor choices of integration limits or similar, essentially representing a quantity that should properly be infinitesimal but is concretely finite by accident. $\endgroup$
    – Roman
    Feb 5 at 18:02
  • $\begingroup$ Thanks, @Roman, let us consider the problem as something like this: Sum[Log[1 + 3 E^-n]/E^-n, {n, 0, 1000}] //N Evaluating that without enough precision did not lead to the correct result. The sum after about 5 terms is ~ 3 for each term. they have a finite contribution to the sum. On the other hand, the first terms in the sums differ from 3. $\endgroup$ Feb 5 at 18:43
0
$\begingroup$

With

number = N[(Log[1 + 3 E^-10000]/E^-10000), 10000] // Quiet;

then

ToExpression[StringTake[ToString[number], 5]]

2.999

You can change 5 in the above to your liking.

The following is pertinent to the comment by @Domen

Round[number]

3

$\endgroup$
5
  • $\begingroup$ Thanks for your answer. In fact, I am looking for some method to get the approximate final result without evaluating internal computations with lots of precisions. Because I do my calculation with that much precision it requires a lot of time to evaluate. Maybe there is no such shortcut. $\endgroup$ Feb 5 at 15:51
  • $\begingroup$ @ghadirjafari not sure if that's possible. to phrase it better, I don't know how to do that $\endgroup$
    – bmf
    Feb 5 at 15:56
  • $\begingroup$ Please look up NumberForm, it is much more robust than string manipulation like you suggest here. $\endgroup$
    – Roman
    Feb 5 at 16:03
  • $\begingroup$ @Roman hey there. thanks for the comment. I am aware of NumberForm. This is the issue for the present case. In other words, it was clear in the OP that the author wanted 2.99 but without a lot of digits of precision. I showed one way to do it. I do not know how to do it with NumberForm and from my understanding of the command it cannot be done. Did you have something more concrete in mind that I fail to see? $\endgroup$
    – bmf
    Feb 5 at 23:35
  • $\begingroup$ Also, please note that used string manipulation but in the end, I obtain a number and hence I can do algebraic manipulations. Is it possible that you missed this bit? $\endgroup$
    – bmf
    Feb 5 at 23:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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