Root √ problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Root √ problem

So I made a basic calculator which can do + - * / ^ √ with 2 numbers can be chosen by user. They all worked perfectly fine except the √. Well in this calculator, any root numbers (odd and even number) of a negative number can't be solved on this calculator, which is not true (Example: square root (√) of -4 or fourth root (4√) of -16 is indeed can't be solved but cubic root (3√) of -64 or fifth root (5√) of -1024 can be solved but in this calculator it is not possible) (I used a way to make "custom" root number beside square or cubic root: "pow(x , 1/y)" ). Summary: Odd root number of negative numbers can be solved but in this calculator it just doesn't do it. So i want to know if there is any way to fix this. Here goes the code: https://code.sololearn.com/cs2saqeQUvLH/?ref=app

13th Nov 2021, 5:03 PM
Blite
2 Answers
+ 1
Did you take into account that roots for negative numbers work very differently from roots for positive numbers? E.g. you need imaginary numbers.
13th Nov 2021, 5:26 PM
Simon Sauter
Simon Sauter - avatar
0
Simon Sauter Well I tested again with cubic root of -10 and as result it say "-nan" but cubic root of 10 still has a valid result tho Test I tried: -Cubic root of 10 = valid (true) -Cubic root of -10 = "-nan" (false) -Square root of 10 = valid (true) -Square root of -10 = "-nan" (true) Well I don't know about how numbers work in C++ like how negative numbers are different from positive ones
14th Nov 2021, 1:13 AM
Blite