Please ignore secret bonuses. Secret tests do NOT award bonus. Max hw grade is 30+2 bonus efficiency

Do you need help?

Notice Board

Per partecipare al corso di Fondamenti di programmazione 2023-24 loggatevi e attivatelo nella vostra pagina dei corsi preferiti. A quel punto il corso appare nel menù personale cliccando sul proprio avatar. Per i materiali degli anni precedenti seguite lo stesso metodo.

To join the Programming/Lab 2023-24 course, log-on and select it on the my courses page. It will appear on the personal menu of your avatar. For earlier years use the same method.

About the 3rd exercise of unit 2 [poll]

Tomiris (1510 points)
2 2 5
in Course info by (1.5k points)

The 3rd exercise of the unit 2 has some complications. 

  • Write a function that gets a value for x as input and returns the value in x of the function image
    • Evaluate the above function for the values 0, 1, 4, -3

The answers are weird:

f(0)-0.809016994374947 - 0.587785252292473*I
>>> f(1)-1.0 + 1.25992104989487*I**0.333333333333333
>>> f(4)-0.744406937295958 - 0.540843298087734*I + 1.25992104989487*I**0.333333333333333
>>> f(-3)
0.577507067769120

Is it okay? Or is it because of Simpy?

### no choices found for poll!
547 views

6 Answers

aryanahamed (7920 points)
6 12 67
by (7.9k points)

Python by default always returns the Principle root, which is a complex number in terms of negative numbers. So in that sense, I assume the answer is correct. 

But if you want the function to return Real root, you need to do some extra steps.

P
PeressiniA (1390 points)
0 0 12
by (1.4k points)
Python has a problem returning the root of negative numbers, so my advice is to always do the root on a positive number and then if needed turn it back to a negative number.

Also as long as you're working with the cubic root in python 3.11 if you import math try using math.cbrt(x), this shouldn't have any problems with negative numbers
Gianmariaromano (5820 points)
0 0 13
by (5.8k points)

Cubic and fifth roots in python are generally given as complex roots, so I suggest you to implement if and else commands to get the real roots.

Nago (1690 points)
7 14 20
by (1.7k points)
edited by

Like said by user aryanahamed and other users, python doesn't return the real root of a number like you would expect to. In order to do that you have to program  some additional steps  so it computes the root as a positive and then turns the number into a negative one after it computes it. 

B
BenattiLorenzo (580 points)
2 3 9
by (580 points)
Try to always compute the root of a positive number. Since in the exercise it's negative, take the positive one and after computing it change the sign
p
prasantaks (900 points)
0 0 7
by (900 points)
in python, there is no problem under the root if you use negative number, because you will get eventually result with complex number
E
Egnald Çela (2320 points)
7 12 29
by (2.3k points)
Hello,

As far as I am aware of, if the code is written correctly, then it is normal for python to have those outputs for your selected inputs, considering that Python has to calculate complex numbers (roots of negative numbers).
n
nerimatt (2320 points)
0 0 20
by (2.3k points)
yes, python will give you complex numbers if you try to calculate the root of negative numbers, the problem is that the root of negative numbers with odd number indices on the root, like cubic roots, are real numbers!

if you try to calculate "(-27)**(1/3)" in python it will give you a complex number, that is much worse to work with than the actual answer of -3!

i think the difficulty of this exercise was to actually get some usable numbers, and not complex ones.