Skip to main content
2 of 5
edited tags; edited title
Alexey Popkov
  • 62.3k
  • 7
  • 154
  • 375

Changing the definition of N: unexpected $RecursionLimit::reclim error

I have some objects represented as follows:

Object[data, param]

data is a list of numbers, param is an integer.

Somewhere in my program, I need to do something like that:

pseed = N[seed, prec];

In this context, seed can be either a number or my object Object. When seed is an Object I want N to be applied only to data list. But N is applied to param too, which I want always to be an Integer.

Of course, I could write:

pseed = If[Head[seed] === Object,
  MapAt[N[#, prec]&, seed, 1],
  N[seed, prec]
];

But I believe, this would make my code messy and hard to read. Therefore I decided to do something like that:

Unprotect[N];
N[Object[data_List, param_Integer], p_] := Object[N[data, p], param];
Protect[N];

Than the simple N[seed, prec] should work whether seed is Obj or Number. However, I get the following error:

$RecursionLimit::reclim: Recursion depth of 1024 exceeded.

I cannot understand, where do I have a recursion?

Thanks in advance!

ktm
  • 539
  • 2
  • 9