- #1
izzy93
- 35
- 0
Hello all,
It's quite a mouthful but I need help writing a function that will behave as follows:
1.if called with an argument which is an integer, it will return the absolute value of the input
2. if called with a real argument, it will return the number rounded to the nearest integer if the argument is positive, zero if the argument is negative.
3. if called with a complex argument, it will round the real part and the imaginary part to the nearest integer and return the modulus of the result.
I starting with something like this.
rr = {a_integer -> Abs[a], a_real -> If[a_real < 0, 0, Integer[a]],
a_complex ->
Then I found the 'which' function
ff[a_] = Which[integer[a], Abs[a], a_Real,
If[a_real < 0, 0, Integer[a]],
a_Complex, {Real[a] & Im[a] -> Mod[integer[a]] }]
However I don't think I'm inputting things correctly.
Any tips would be appreciated
Thanks
It's quite a mouthful but I need help writing a function that will behave as follows:
1.if called with an argument which is an integer, it will return the absolute value of the input
2. if called with a real argument, it will return the number rounded to the nearest integer if the argument is positive, zero if the argument is negative.
3. if called with a complex argument, it will round the real part and the imaginary part to the nearest integer and return the modulus of the result.
I starting with something like this.
rr = {a_integer -> Abs[a], a_real -> If[a_real < 0, 0, Integer[a]],
a_complex ->
Then I found the 'which' function
ff[a_] = Which[integer[a], Abs[a], a_Real,
If[a_real < 0, 0, Integer[a]],
a_Complex, {Real[a] & Im[a] -> Mod[integer[a]] }]
However I don't think I'm inputting things correctly.
Any tips would be appreciated
Thanks