#Doubt

108 messages · Page 1 of 1 (latest)

subtle oriole
#

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

burnt mothBOT
#

This post has been reserved for your question.

Hey @subtle oriole! Please use /close or the Close Post button 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.

subtle oriole
#

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

lusty palm
#

NaN is js

subtle oriole
#

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));
    }
}```
lusty palm
#

/run ```java
System.out.println(Math.sqrt(36-45));

vagrant jacinthBOT
#

Here is your java(15.0.2) output @lusty palm

NaN
subtle oriole
#

^

lusty palm
#

my bad, I assumed java would throw exception

subtle oriole
#

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

queen adder
queen adder
subtle oriole
#

I was wondering the same, but again I tried searching it up and all of it said runtime error

queen adder
#

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

subtle oriole
#

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

queen adder
#

"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

queen adder
subtle oriole
burnt mothBOT
# subtle oriole 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.

queen adder
#

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

subtle oriole
#

but isn't -0.0 or 0 the same

queen adder
#

if you have 0.0/0.0, you can't know which one is actually smaller, so that turns into NaN

queen adder
subtle oriole
subtle oriole
#

I did try compiling 1.0/-0, however it returned Infinity

subtle oriole
queen adder
subtle oriole
#

/run
System.out.println(1.0/-0);

vagrant jacinthBOT
#

@subtle oriole

Error

Invalid command format (missing codeblock?)

subtle oriole
#

/run

System.out.println(1.0/-0)```
vagrant jacinthBOT
subtle oriole
#

/run

System.out.println(1.0/-0)```
vagrant jacinthBOT
#

@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
subtle oriole
#

/run

System.out.println(1.0/-0);```
vagrant jacinthBOT
#

Here is your java(15.0.2) output @subtle oriole

Infinity
subtle oriole
#

sorry for the mess-

queen adder
#

-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

subtle oriole
#

oh alright fair -0.0 returns -Infinity

queen adder
#

/run

System.out.println(1.0/-0.0);
vagrant jacinthBOT
#

Here is your java(15.0.2) output @queen adder

-Infinity
queen adder
#

yeah

queen adder
subtle oriole
#

so its no error, that is fair

queen adder
#

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

subtle oriole
burnt mothBOT
subtle oriole
#

do I close this?

queen adder
#

if you're satisfied with my answer, sure i guess
i probably wouldn't be lol

subtle oriole
queen adder
#

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

queen adder
#

nah that's definitely not your bad lol

subtle oriole
#

and I really appreciate your help and patience

queen adder
#

what sources did you find that stated it to be a "runtime error"? im curious on how that came up

subtle oriole
#

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

queen adder
#

"Run time error"
peko_yuck

lusty palm
queen adder
#

the spacing there is... wow

#

yeah i would not trust this source

subtle oriole
subtle oriole
#

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

queen adder
#

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

queen adder
subtle oriole
#

oh

queen adder
#

"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

subtle oriole
#

oh alright

#

thank you again

burnt mothBOT
# subtle oriole thank you again

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.

quiet cloak
quiet cloak
#

i and @subtle oriole and technically the students of our batch were confused af 💀

queen adder
#

could you like not spam ping me thanks

quiet cloak
subtle oriole
#

yes please, im sorry on his behalf

quiet cloak