Math.sqrt(-1) returns NaN. What type of error should this be? At the first glance, it looks like a logical error, since root of a negative number(negative 1, here) is i(mathematical constant). I had this question in a test as well, however the right answer was marked as "runtime", and google says the same. I'm not saying google is 100% authentic but I didn't find any answers saying logical error
#Doubt
108 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @subtle oriole! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
This is not a JavaScript server. [Java](https://en.wikipedia.org/wiki/Java_(programming_language) and JavaScript are completely different languages. See: How is JavaScript different from Java?
yeah, im asking about java, not javascript
I don't see myself mentioning that my query concerns javascript and not java, and since it is a java server the latter should be more obvious, no offense
NaN is js
it is java as well
unless you say bluej isn't authentic
import java.util.*;
class Example {
public static void main() {
print p=new print();
System.out.println(Math.sqrt(36-45));
}
}```
/run ```java
System.out.println(Math.sqrt(36-45));
Here is your java(15.0.2) output @lusty palm
NaN
^
my bad, I assumed java would throw exception
its alright lol it happens.. the fact that it does not throw an exception is why I thought it would be a logical error
but it turns out to be a runtime error which doesn't really make sense to me
NaN is IEEE 754
if by "runtime error" it means a RuntimeException, it shouldn't be that
I was wondering the same, but again I tried searching it up and all of it said runtime error
i'd say it's an arithmatic/mathematical error in the same vein as 1.0/0
it's an issue that occurs at runtime, but it's not an Exception
that's the main issue here tbh, "error" is vague with java
right
also any idea why 0.0/0, or any double by 0 returns infinity/NaN but when you make it an integer by 0 it returns an arithmetic error?
35.0/0 would return infinity but 35/0 would be an arithmetic exception
"error" could mean 1 of 3 things
- any issue at all
- a compiler error
- a runtime error, perhaps matching to a class
a language like js might be less vague here, as its "issue" class is Error
but java's Error is much more niche, and general issues use Exception instead
1.0/0 is doing floating-point arithmetic in accordance to IEEE 754 as i mentioned before, so it can represent that as Infinity, but 1/0 is integer arithmetic, where it is undefined
so, the Math.sqrt of a negative number is a logical error, right?
oh, alright
thank you
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
floating-points don't have a true representation of mathematical 0, every value is an approximation, there's limited precision
so if you had a really really small number, that would get turned into 0
a random number divided by a really small number is a really big number, which is represented by Infinity
this is also why -0 exists, very small number that is negative, 1.0/-0.0 is -Infinity
yeah that makes sense
oh
but isn't -0.0 or 0 the same
if you have 0.0/0.0, you can't know which one is actually smaller, so that turns into NaN
in math yes, for floating-points no
oh alright
oh
https://www.youtube.com/watch?v=dQhj5RGtag0
this may be of interest
a description of the IEEE single-precision floating point standard
http://patreon.com/hbmmaster
http://conlangcritic.bandcamp.com
http://seximal.net
http://twitter.com/hbmmaster
http://janmisali.tumblr.com
I did try compiling 1.0/-0, however it returned Infinity
thank you, ill check it out in a bit
hard to say. "logical error" isn't well-defined, it seems quite subjective imo
/run
System.out.println(1.0/-0);
@subtle oriole
Error
Invalid command format (missing codeblock?)
/run
System.out.println(1.0/-0)```
@subtle oriole
Error
Unsupported language: None
Request a new language
/run
System.out.println(1.0/-0)```
@subtle oriole I only received java(15.0.2) error output
file0.code.java:2: error: ';' expected
System.out.println(1.0/-0)
^
1 error
error: compilation failed
/run
System.out.println(1.0/-0);```
Here is your java(15.0.2) output @subtle oriole
Infinity
sorry for the mess-
-0 is the unary operator - applied to an integer 0, which is exact and yields 0
then 0 is coerced to the double 0.0 for the operation
oh alright fair -0.0 returns -Infinity
/run
System.out.println(1.0/-0.0);
Here is your java(15.0.2) output @queen adder
-Infinity
so, no error?
yeah
that loops back to this really
oh alright
so its no error, that is fair
if by "error" you mean "issue", then sure, it could be considered a logical error
but if by "error" you mean Exception, then no, there's no error here, just an issue
strictly adhering to terms, the latter would be more correct and useful, but the former tends to be used more loosely
so it's really hard to say here
makes sense to me now, thank you so much for the time
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
do I close this?
if you're satisfied with my answer, sure i guess
i probably wouldn't be lol
I didn't want to go into too much depth, I just had this in an exam last time and while revising I saw this again, so I was pretty confused what it should have been
if im being honest, sounds like a badly-written question, it's not specific enough
probably relies on how the teacher/curriculum specifically taught it rather than more exact terms
my bad on that
nah that's definitely not your bad lol
we have pretty basic java stuff, I was stuck on this and that's all I was wondering, I just needed a clarification over the fact that whether it was a runtime error or not (since it made no sense to be so, yet all sources cited to the same answer)
and I really appreciate your help and patience
what sources did you find that stated it to be a "runtime error"? im curious on how that came up
by sources, I basically mean all answers I found on google, which im not saying is authentic again, but that's what came up
stuff like this is misleading, I could say
"Run time error"
yeah I would expect an error reading this
yeah doesn't make sense at all
right, pretty misleading 😭
this doesn't make sense either
I did find 1 answer which said logical, but again majority of the answers cited towards runtime which led to me getting a bit confused 😅
chatgpt said runtime as well, for some reason
using these definitions, it's at least self-consistent at least
"runtime error" still isn't correct imo, but it's the least wrong option from these
if this was an option it'd probably be this instead
oh
"logical error" is more commonly used for stuff like ifs/whiles/fors being set up wrong for example
so yeah i can see why that isn't the "correct" one for this
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
true
yeah
that's true too
cool
ikr lol
i and @subtle oriole and technically the students of our batch were confused af 💀
could you like not spam ping me thanks
whoops sorry i should have disabled mentioned replies sry
yes please, im sorry on his behalf
uh
