# Tag Info

53

Control the Precision and Accuracy of Numerical Results This is an excellent question. Of course everyone could claim highest accuracy for her product. To deal with this situation there exist benchmarks to test for accuracy. One such benchmark is from NIST. This specific benchmark deals with the accuracy of statistical software for instance. The NIST ...

21

UpArrow[a_, n_Integer] := Nest[a^# &, 1, n] then UpArrow[4, 3] or 4 \[UpArrow] 3 To complete this method you may wish to add an input alias: AppendTo[CurrentValue[$FrontEndSession, InputAliases], "up" -> "\[UpArrow]"]; Now EscupEsc will enter \[UpArrow]. Change$FrontEndSession to $FrontEnd and run it only once to make the change ... 19 Stan Wagon presents a little utility function in his book Mathematica in Action called IntegerChop[]. Here's a slightly wrinkled version: IntegerChop = With[{r = Round[#]}, r + Chop[# - r]] &; You might wish to do comparisons yourself (the computer I am using does not have Mathematica). Here are some benchmarks: Do[roundif /@ testdata, {10000}]; //... 19 I'm surprised there isn't a question about this (i.e. entering numbers in scientific notation) already. To enter$3\times10^{-3}$, you can write 3*^-3. For further reference, see Input Syntax: Numbers. 15 Here is a definition for mixedForm that works for all cases, i.e. proper and improper fractions and integers. Clear[mixedForm] mixedForm[Rational[x_, y_]] := If[Abs@x > y, HoldForm[#1 + #2/y], x/y] & @@ (Sign@x QuotientRemainder[Abs@x, y]) mixedForm[x_Integer] := x Some examples: mixedForm /@ {2, 4/5, 10/3, -3/4, -5/2} Out[1]= {2, 4/5, 3 + 1/... 13 The function converting strings to integer is FromDigits. It is the counterpart of IntegerString and both functions can be used with whatever basis you like. Therefore, if you want to convert from base 16 you do FromDigits["6b", 16] 13 IntegerDigits works Try powers = IntegerDigits[204, 2] {1, 1, 0, 0, 1, 1, 0, 0} Now, if you want that formatted as a sum of powers of two, you have to hold it. For example Total@MapIndexed[#1 Defer[2]^(First@#2 - 1) &, Reverse@powers] 2^2 + 2^3 + 2^6 + 2^7 EDIT Nicer code, given that your numbers go up to 255 pow2[num_]:=Inner[#1 2^... 13 Thanks to Nasser M. Abbasi i found a way. To change the Display. The function that you can provide for any ~Legend via LegendFunction wraps the complete ~Legend[] into anything. And he mentioned, that NumberForm encapsulates the numbers. So why not replace them (delayed)? Version 1: Scientific Notation at every Label Hence choosing f[x_] := x /. {... 13 One way is to plot the function 0 against a log axis. LogLogPlot[0, {t, 1, 12}, Axes -> {True, False}, Ticks -> {Range[12]}] or, changing the numbers LogLogPlot[0, {t, 64, 96}, Axes -> {True, False}, Ticks -> {Range[64, 96]}] The Axis function turns off the vertical axis (because you just want the number line) and the Ticks specifies ... 13 In some settings the integers, fractions, rational numbers, reals, and complexes are five distinct systems. Further, for reals and complexes, there are the standard reals and complexes as well as nonstandard systems. There are mappings from some to others, so that a subset of the reals in an isomorphic image of the integers (as rings), and so on for${\bf Z}...

11

You are losing hugely due to a base 10 implementation. Integers are manipulated in base 2 in Mathematica. So you would want a base 2 variant to get any reasonable behavior. Here is one way to code it. There might be tweaks that improve it. fastSquare[a_] := Catch[Module[ {len = BitLength[a], len2, hi, lo, h2, l2, hl}, If[len < 100, Throw[a^2]]; ...

11

As noted in post, responses and comments, Real is a Mathematica head and, as such, is distinct from Integer and Rational and Complex. All of these are regarded as "atomic" (notwitstanding that Rational has two Integer "parts", and Complex is comprised of any mix of the other three types). These atomic types are in a sense distinct from the domains one ...

10

EDIT As Chris pointed out Floor used this way fails for cases where the value is slightly less than a whole number, whereas Round works. I shall edit the remainder of my answer to correct this oversight. If you are using x == 0 you shouldn't need Chop since it is already making a numeric comparison: If[# - Round[#] == 0, Round[#], #] & Or simply: ...

10

N A one-character answer is disallowed by SE, so I will expand. N is mostly what I use. If I have an expression like $2 x + 3$, I sometimes write it 2. x + 3. in Mathematica; then if x is numeric, whether it happens to be an Integer or not, the expression will always be Real or Complex.

9

Some of the above don't work in some cases due to machine approximation, e.g. x = 6250*0.292 1825. If[# - ⌊#⌋ == 0, Round@#, #] &[x] 1825. Chop[# - ⌊#⌋] + ⌊#⌋ &[x] 1825. IntegerPart@# + Chop@FractionalPart@# &[x] 1825. But Stan Wagon's method works: With[{r = Round[#]}, r + Chop[# - r]] &[x] 1825

8

My original answer is incorrect -- it is preserved as a record of my own hubris. :^) Simply, as Rojo points out, the calculation is still being done with 1*^1000, it's just being done at a different time. One may see this by manually observing the time taken for evaluation on an idle machine, or by setting this option which will print the total time taken ...

8

I have faced this problem earlier but failed attempts with simple operations based on NumberForm, Round.. have forced me to stop looking for general solution. I have thought my skills in MMA were too low, but also today I am not able to do this in simple way. (haven't I learned anything? :)) This form of expression uncertainty in measurement is described ...

8

There is a tolerance Internal$EqualTolerance that is applied when testing Real numbers. If the numbers agree up to the last Internal$EqualTolerance digits, then they are treated as equal. Try this: eps = 1.0; p = 0; Block[{Internal\$EqualTolerance = 0.}, While[(1.0 + eps) > 1.0, eps = eps/2.0; p += 1]; ] eps p eps*2 (* 1.11022*10^-16 53 2....

8

Here's a more general variant a(↑...↑)b with any given number of up-arrows, as defined on MathWorld: (* Short-hand for single arrow. *) UpArrow[a_, b_] := UpArrow[1][a, b]; (* Trivial case of a(↑...↑)1. *) UpArrow[_][a_, 1] := a; (* Single arrow: exponentation. *) UpArrow[1][a_, b_] := a^b; (* Generic case: do a recursion. *) UpArrow[n_Integer][a_, ...

8

May be I'm overthinking this one. list = {0, 1, -2, 259}; f[x_, y_] := x + (2 Boole@NonNegative[x] - 1 ) y/10^IntegerLength[y]//N ; list /. {a_, b_, c_, d_} -> {f[a, b], f[c, d]} (* {0.1, -2.259} *)

8

PlusMinus[{x_, err_}] := Module[{errE = Last@MantissaExponent[err], xE = Last@MantissaExponent[x]}, Row[{"(", NumberForm[N@Round[x, 10^(errE - 1)]*10^(-xE + 1), {xE - errE + 1, xE - errE}], " \[PlusMinus] ", NumberForm[N@Round[err, 10^(errE - 1)]*10^(-xE + 1), {1, xE - errE}, ExponentFunction -> (Null &)], ")", " \[Times]...

8

As noted in the comments this behavior follows from the definition of FromDigits, though I only understood this myself within the last year or two when someone* used it to boost performance. Consider a symbolic example: sym = FromDigits[{a, b, c}] sym /. { a -> {a1, a2, a3, a4, a5}, b -> {b1, b2, b2, b4, b5}, c -> {c1, c2, c3, c4, ...

7

I like this for readability: roundif = IntegerPart@# + Chop@FractionalPart@# & It's also listable and fairly fast.

7

Ok, if you want it faster still, and your close to integer numbers are machine-size integers - here are two equivalent implementations - in Mathematica compiled to C, and Java. It is an interesting problem to compare performance, we will observe that Java code is speed-equivalent to C code here, modulo small extra time needed for data transfer. The idea is ...

7

stringToHex[str_] := ToExpression["16^^" <> str]; This is just a way of automating the normal notation you would use, which is 16^^6b (check here for the documentation).

7

Another solution based on FractionalPart and IntegerPart would be : Fraction[x_Rational]:= Function[{z, y}, If[z!=0, HoldForm[z + y], HoldForm[y]], {HoldAll}] @@ {IntegerPart[x], FractionalPart[x]} Fraction[x_Integer] := x This approach produces slightly different results than R.M.'s solution : Fraction /@ {2, 4/5, 10/3, -...

7

A simple solution is to use Rationalize on the result. If your original data is all integers, but with the head Real (e.g. {1., 2., 3.}) then they'll have the head Integer now (i.e., {1, 2, 3}).

7

From FromDigits>>Details: For example: N@FromDigits[RealDigits[123.55555]] (* 123.556 *)

7

The Mathematica parser parses and computes number literals before sending them to FullForm even if Hold is applied. Thus the full form of number literals is not accessible to the user. You are making assumptions that are incorrect. When Mathematica reads either 4 or 2^^100, it parses both to the exact same in-memory representation. After the parsing step,...

6

This isn't directly an answer, and I'll delete it if it is off target. But you might want to use some non-System context functionality for taking polynomial-mod-2 products. Specifically this works with integer lists of coefficients. I'll show an example below. In[1110]:= SeedRandom[1111]; vals = RandomInteger[2^8 - 1, 2] intlists = Map[Reverse[...

Only top voted, non community-wiki answers of a minimum length are eligible