Skip to main content
Corrected typo
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

You don't need to define any functions. You just need to write a While-loop.

It is tempting to write the While-loop to use system floating-point arithmetic for speed, like so:

With[{ϵ = 10.^-16},
  Block[{x = 1., nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[], x = nxt]];
    nxt]]

But this doesn't work because system floating-point arithmetic can't maintain 16 digits of precision over the iteration. To avoid this numerics problem, the While-loop can be written to compute with exact numbers. The final value will be converted to a 16-digit arbitrary precision number.

With[{ϵ = 10^-16},
  Block[{x = 1, nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[], x = nxt]];
  N[nxt, 16]]]

0.7390851332151606

The above can be simplified a little by using an undocumented feature of Break. It returns its argument when given one.

With[{ϵ = 10^-16},
  Block[{x = 1, nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[N[nxt, 16]], x = nxt]]]]

The code editor complains about this use of Break, but the evaluatesevaluator accepts it and it works fine.

I also feel I should point out that it is better Mathematica practice to use FixedPoint than to write a While-loop.

With[{ϵ = 10^-16},
  N[FixedPoint[Cos, 1, SameTest -> (Abs[#1 - #2] < ϵ &)], 16]]

0.7390851332151606

You don't need to define any functions. You just need to write a While-loop.

It is tempting to write the While-loop to use system floating-point arithmetic for speed, like so:

With[{ϵ = 10.^-16},
  Block[{x = 1., nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[], x = nxt]];
    nxt]]

But this doesn't work because system floating-point arithmetic can't maintain 16 digits of precision over the iteration. To avoid this numerics problem, the While-loop can be written to compute with exact numbers. The final value will be converted to a 16-digit arbitrary precision number.

With[{ϵ = 10^-16},
  Block[{x = 1, nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[], x = nxt]];
  N[nxt, 16]]]

0.7390851332151606

The above can be simplified a little by using an undocumented feature of Break. It returns its argument when given one.

With[{ϵ = 10^-16},
  Block[{x = 1, nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[N[nxt, 16]], x = nxt]]]]

The code editor complains about this use of Break, but the evaluates accepts it and it works fine.

I also feel I should point out that is better Mathematica practice to use FixedPoint than to write a While-loop.

With[{ϵ = 10^-16},
  N[FixedPoint[Cos, 1, SameTest -> (Abs[#1 - #2] < ϵ &)], 16]]

0.7390851332151606

You don't need to define any functions. You just need to write a While-loop.

It is tempting to write the While-loop to use system floating-point arithmetic for speed, like so:

With[{ϵ = 10.^-16},
  Block[{x = 1., nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[], x = nxt]];
    nxt]]

But this doesn't work because system floating-point arithmetic can't maintain 16 digits of precision over the iteration. To avoid this numerics problem, the While-loop can be written to compute with exact numbers. The final value will be converted to a 16-digit arbitrary precision number.

With[{ϵ = 10^-16},
  Block[{x = 1, nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[], x = nxt]];
  N[nxt, 16]]]

0.7390851332151606

The above can be simplified a little by using an undocumented feature of Break. It returns its argument when given one.

With[{ϵ = 10^-16},
  Block[{x = 1, nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[N[nxt, 16]], x = nxt]]]]

The code editor complains about this use of Break, but the evaluator accepts it and it works fine.

I also feel I should point out that it is better Mathematica practice to use FixedPoint than to write a While-loop.

With[{ϵ = 10^-16},
  N[FixedPoint[Cos, 1, SameTest -> (Abs[#1 - #2] < ϵ &)], 16]]

0.7390851332151606

deleted 1 character in body
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

You don't need to define any functions. You just need to write a While-loop.

It is tempting to write the While-loop to use system floating-point arithmetic for speed, like so:

With[{ϵ = 10.^-16},
  Block[{x = 1., nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[], x = nxt]];
    nxt]]

But this doesn't work because system floating-point arithmetic can't maintain 16 digits of precision over the iteration. To avoid this numerics problem, the While-loop can be written to compute with exact numbers. The final value will be converted to a 16-digit arbitrary precision number.

With[{ϵ = 10^-16},
  Block[{x = 1, nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[], x = nxt]];
  N[nxt, 16]]]

0.7390851332151606

The above can be simplified a little by using an undocumented feature of Break. It returns its argument when given one.

With[{ϵ = 10^-16},
  Block[{x = 1, nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[N[nxt, 16]], x = nxt]]]]

The code editor complains about this use of Break, but the evaluates accepts it and it works fine.

I also feel I should point out that is better Mathematica practice to use FixedPoint than to write a While-loop.

With[{ϵ = 10^-16},
  N[FixedPoint [CosN[FixedPoint[Cos, 1, SameTest -> (Abs[#1 - #2] < ϵ &)], 16]]

0.7390851332151606

You don't need to define any functions. You just need to write a While-loop.

It is tempting to write the While-loop to use system floating-point arithmetic for speed, like so:

With[{ϵ = 10.^-16},
  Block[{x = 1., nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[], x = nxt]];
    nxt]]

But this doesn't work because system floating-point arithmetic can't maintain 16 digits of precision over the iteration. To avoid this numerics problem, the While-loop can be written to compute with exact numbers. The final value will be converted to a 16-digit arbitrary precision number.

With[{ϵ = 10^-16},
  Block[{x = 1, nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[], x = nxt]];
  N[nxt, 16]]]

0.7390851332151606

The above can be simplified a little by using an undocumented feature of Break. It returns its argument when given one.

With[{ϵ = 10^-16},
  Block[{x = 1, nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[N[nxt, 16]], x = nxt]]]]

The code editor complains about this use of Break, but the evaluates accepts it and it works fine.

I also feel I should point out that is better Mathematica practice to use FixedPoint than to write a While-loop.

With[{ϵ = 10^-16},
  N[FixedPoint [Cos, 1, SameTest -> (Abs[#1 - #2] < ϵ &)], 16]]

0.7390851332151606

You don't need to define any functions. You just need to write a While-loop.

It is tempting to write the While-loop to use system floating-point arithmetic for speed, like so:

With[{ϵ = 10.^-16},
  Block[{x = 1., nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[], x = nxt]];
    nxt]]

But this doesn't work because system floating-point arithmetic can't maintain 16 digits of precision over the iteration. To avoid this numerics problem, the While-loop can be written to compute with exact numbers. The final value will be converted to a 16-digit arbitrary precision number.

With[{ϵ = 10^-16},
  Block[{x = 1, nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[], x = nxt]];
  N[nxt, 16]]]

0.7390851332151606

The above can be simplified a little by using an undocumented feature of Break. It returns its argument when given one.

With[{ϵ = 10^-16},
  Block[{x = 1, nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[N[nxt, 16]], x = nxt]]]]

The code editor complains about this use of Break, but the evaluates accepts it and it works fine.

I also feel I should point out that is better Mathematica practice to use FixedPoint than to write a While-loop.

With[{ϵ = 10^-16},
  N[FixedPoint[Cos, 1, SameTest -> (Abs[#1 - #2] < ϵ &)], 16]]

0.7390851332151606

Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

You don't need to define any functions. You just need to write a While-loop.

It is tempting to write the While-loop to use system floating-point arithmetic for speed, like so:

With[{ϵ = 10.^-16},
  Block[{x = 1., nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[], x = nxt]];
    nxt]]

But this doesn't work because system floating-point arithmetic can't maintain 16 digits of precision over the iteration. To avoid this numerics problem, the While-loop can be written to compute with exact numbers. The final value will be converted to a 16-digit arbitrary precision number.

With[{ϵ = 10^-16},
  Block[{x = 1, nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[], x = nxt]];
  N[nxt, 16]]]

0.7390851332151606

The above can be simplified a little by using an undocumented feature of Break. It returns its argument when given one.

With[{ϵ = 10^-16},
  Block[{x = 1, nxt},
    While[True,
      nxt = Cos[x];
      If[Abs[nxt - x] < ϵ, Break[N[nxt, 16]], x = nxt]]]]

The code editor complains about this use of Break, but the evaluates accepts it and it works fine.

I also feel I should point out that is better Mathematica practice to use FixedPoint than to write a While-loop.

With[{ϵ = 10^-16},
  N[FixedPoint [Cos, 1, SameTest -> (Abs[#1 - #2] < ϵ &)], 16]]

0.7390851332151606