Why does casting infinity into byte or short equal -1 in Java? [duplicate]












1















This question already has an answer here:




  • Casting a double to another numeric type

    1 answer




Recently I found out that if you take a double or float and divide it by 0 that you get infinity as value. I converted infinity to every datatype (by writing (byte)variable, (short)variable etc.) and I found out that if you convert it into byte or short it gives the value -1, but if you convert it into any other datatype it gives you the max value of it back. I was wondering why that happened. Anyone got a guess?



Here is the code im using to test it.



double dsa = 1.00 / 0;

System.out.println("Byte");
System.out.println((byte)dsa);
System.out.println(Byte.MAX_VALUE + "n");

System.out.println("Short");
System.out.println((short)dsa);
System.out.println(Short.MAX_VALUE + "n");

System.out.println("Integer");
System.out.println((int)dsa);
System.out.println(Integer.MAX_VALUE + "n");

System.out.println("Long");
System.out.println((long)dsa);
System.out.println(Long.MAX_VALUE + "n");

System.out.println("Float");
System.out.println((float)dsa);
System.out.println(Float.MAX_VALUE + "n");

System.out.println("Double");
System.out.println((double)dsa);
System.out.println(Double.MAX_VALUE + "n");


And here is what the console gives me:



Byte
-1
127

Short
-1
32767

Integer
2147483647
2147483647

Long
9223372036854775807
9223372036854775807

Float
Infinity
3.4028235E38

Double
Infinity
1.7976931348623157E308









share|improve this question















marked as duplicate by Sotirios Delimanolis java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 14:43


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 2




    "I converted infinity" - Can we see your code?
    – Jacob G.
    Nov 20 '18 at 14:31






  • 1




    Did you mean "casted" instead of "converted"?
    – m0skit0
    Nov 20 '18 at 14:32










  • I added the code to it and I might mean casted instead. sorry i dont learn about programming in english so im not sure. just look at the code.
    – Lino1000
    Nov 20 '18 at 14:34
















1















This question already has an answer here:




  • Casting a double to another numeric type

    1 answer




Recently I found out that if you take a double or float and divide it by 0 that you get infinity as value. I converted infinity to every datatype (by writing (byte)variable, (short)variable etc.) and I found out that if you convert it into byte or short it gives the value -1, but if you convert it into any other datatype it gives you the max value of it back. I was wondering why that happened. Anyone got a guess?



Here is the code im using to test it.



double dsa = 1.00 / 0;

System.out.println("Byte");
System.out.println((byte)dsa);
System.out.println(Byte.MAX_VALUE + "n");

System.out.println("Short");
System.out.println((short)dsa);
System.out.println(Short.MAX_VALUE + "n");

System.out.println("Integer");
System.out.println((int)dsa);
System.out.println(Integer.MAX_VALUE + "n");

System.out.println("Long");
System.out.println((long)dsa);
System.out.println(Long.MAX_VALUE + "n");

System.out.println("Float");
System.out.println((float)dsa);
System.out.println(Float.MAX_VALUE + "n");

System.out.println("Double");
System.out.println((double)dsa);
System.out.println(Double.MAX_VALUE + "n");


And here is what the console gives me:



Byte
-1
127

Short
-1
32767

Integer
2147483647
2147483647

Long
9223372036854775807
9223372036854775807

Float
Infinity
3.4028235E38

Double
Infinity
1.7976931348623157E308









share|improve this question















marked as duplicate by Sotirios Delimanolis java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 14:43


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 2




    "I converted infinity" - Can we see your code?
    – Jacob G.
    Nov 20 '18 at 14:31






  • 1




    Did you mean "casted" instead of "converted"?
    – m0skit0
    Nov 20 '18 at 14:32










  • I added the code to it and I might mean casted instead. sorry i dont learn about programming in english so im not sure. just look at the code.
    – Lino1000
    Nov 20 '18 at 14:34














1












1








1








This question already has an answer here:




  • Casting a double to another numeric type

    1 answer




Recently I found out that if you take a double or float and divide it by 0 that you get infinity as value. I converted infinity to every datatype (by writing (byte)variable, (short)variable etc.) and I found out that if you convert it into byte or short it gives the value -1, but if you convert it into any other datatype it gives you the max value of it back. I was wondering why that happened. Anyone got a guess?



Here is the code im using to test it.



double dsa = 1.00 / 0;

System.out.println("Byte");
System.out.println((byte)dsa);
System.out.println(Byte.MAX_VALUE + "n");

System.out.println("Short");
System.out.println((short)dsa);
System.out.println(Short.MAX_VALUE + "n");

System.out.println("Integer");
System.out.println((int)dsa);
System.out.println(Integer.MAX_VALUE + "n");

System.out.println("Long");
System.out.println((long)dsa);
System.out.println(Long.MAX_VALUE + "n");

System.out.println("Float");
System.out.println((float)dsa);
System.out.println(Float.MAX_VALUE + "n");

System.out.println("Double");
System.out.println((double)dsa);
System.out.println(Double.MAX_VALUE + "n");


And here is what the console gives me:



Byte
-1
127

Short
-1
32767

Integer
2147483647
2147483647

Long
9223372036854775807
9223372036854775807

Float
Infinity
3.4028235E38

Double
Infinity
1.7976931348623157E308









share|improve this question
















This question already has an answer here:




  • Casting a double to another numeric type

    1 answer




Recently I found out that if you take a double or float and divide it by 0 that you get infinity as value. I converted infinity to every datatype (by writing (byte)variable, (short)variable etc.) and I found out that if you convert it into byte or short it gives the value -1, but if you convert it into any other datatype it gives you the max value of it back. I was wondering why that happened. Anyone got a guess?



Here is the code im using to test it.



double dsa = 1.00 / 0;

System.out.println("Byte");
System.out.println((byte)dsa);
System.out.println(Byte.MAX_VALUE + "n");

System.out.println("Short");
System.out.println((short)dsa);
System.out.println(Short.MAX_VALUE + "n");

System.out.println("Integer");
System.out.println((int)dsa);
System.out.println(Integer.MAX_VALUE + "n");

System.out.println("Long");
System.out.println((long)dsa);
System.out.println(Long.MAX_VALUE + "n");

System.out.println("Float");
System.out.println((float)dsa);
System.out.println(Float.MAX_VALUE + "n");

System.out.println("Double");
System.out.println((double)dsa);
System.out.println(Double.MAX_VALUE + "n");


And here is what the console gives me:



Byte
-1
127

Short
-1
32767

Integer
2147483647
2147483647

Long
9223372036854775807
9223372036854775807

Float
Infinity
3.4028235E38

Double
Infinity
1.7976931348623157E308




This question already has an answer here:




  • Casting a double to another numeric type

    1 answer








java type-conversion byte short infinity






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 14:38







Lino1000

















asked Nov 20 '18 at 14:30









Lino1000Lino1000

92




92




marked as duplicate by Sotirios Delimanolis java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 14:43


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Sotirios Delimanolis java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 14:43


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    "I converted infinity" - Can we see your code?
    – Jacob G.
    Nov 20 '18 at 14:31






  • 1




    Did you mean "casted" instead of "converted"?
    – m0skit0
    Nov 20 '18 at 14:32










  • I added the code to it and I might mean casted instead. sorry i dont learn about programming in english so im not sure. just look at the code.
    – Lino1000
    Nov 20 '18 at 14:34














  • 2




    "I converted infinity" - Can we see your code?
    – Jacob G.
    Nov 20 '18 at 14:31






  • 1




    Did you mean "casted" instead of "converted"?
    – m0skit0
    Nov 20 '18 at 14:32










  • I added the code to it and I might mean casted instead. sorry i dont learn about programming in english so im not sure. just look at the code.
    – Lino1000
    Nov 20 '18 at 14:34








2




2




"I converted infinity" - Can we see your code?
– Jacob G.
Nov 20 '18 at 14:31




"I converted infinity" - Can we see your code?
– Jacob G.
Nov 20 '18 at 14:31




1




1




Did you mean "casted" instead of "converted"?
– m0skit0
Nov 20 '18 at 14:32




Did you mean "casted" instead of "converted"?
– m0skit0
Nov 20 '18 at 14:32












I added the code to it and I might mean casted instead. sorry i dont learn about programming in english so im not sure. just look at the code.
– Lino1000
Nov 20 '18 at 14:34




I added the code to it and I might mean casted instead. sorry i dont learn about programming in english so im not sure. just look at the code.
– Lino1000
Nov 20 '18 at 14:34












1 Answer
1






active

oldest

votes


















-1














Because positive infinity is defined as 0x7ff0000000000000. From Double class source code:



/**
* A constant holding the positive infinity of type
* {@code double}. It is equal to the value returned by
* {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
*/
public static final double POSITIVE_INFINITY = 1.0 / 0.0;


Also please note that casting is not converting.






share|improve this answer





















  • sorry if it might sound stupid to you or something but why does 0x7ff0000000000000 give -1 in byte or short but the max value in other types?
    – Lino1000
    Nov 20 '18 at 14:43










  • Sorry, my bad, I somehow read 0 instead of -1 :/
    – m0skit0
    Nov 20 '18 at 14:46


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














Because positive infinity is defined as 0x7ff0000000000000. From Double class source code:



/**
* A constant holding the positive infinity of type
* {@code double}. It is equal to the value returned by
* {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
*/
public static final double POSITIVE_INFINITY = 1.0 / 0.0;


Also please note that casting is not converting.






share|improve this answer





















  • sorry if it might sound stupid to you or something but why does 0x7ff0000000000000 give -1 in byte or short but the max value in other types?
    – Lino1000
    Nov 20 '18 at 14:43










  • Sorry, my bad, I somehow read 0 instead of -1 :/
    – m0skit0
    Nov 20 '18 at 14:46
















-1














Because positive infinity is defined as 0x7ff0000000000000. From Double class source code:



/**
* A constant holding the positive infinity of type
* {@code double}. It is equal to the value returned by
* {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
*/
public static final double POSITIVE_INFINITY = 1.0 / 0.0;


Also please note that casting is not converting.






share|improve this answer





















  • sorry if it might sound stupid to you or something but why does 0x7ff0000000000000 give -1 in byte or short but the max value in other types?
    – Lino1000
    Nov 20 '18 at 14:43










  • Sorry, my bad, I somehow read 0 instead of -1 :/
    – m0skit0
    Nov 20 '18 at 14:46














-1












-1








-1






Because positive infinity is defined as 0x7ff0000000000000. From Double class source code:



/**
* A constant holding the positive infinity of type
* {@code double}. It is equal to the value returned by
* {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
*/
public static final double POSITIVE_INFINITY = 1.0 / 0.0;


Also please note that casting is not converting.






share|improve this answer












Because positive infinity is defined as 0x7ff0000000000000. From Double class source code:



/**
* A constant holding the positive infinity of type
* {@code double}. It is equal to the value returned by
* {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
*/
public static final double POSITIVE_INFINITY = 1.0 / 0.0;


Also please note that casting is not converting.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 14:37









m0skit0m0skit0

18.5k95591




18.5k95591












  • sorry if it might sound stupid to you or something but why does 0x7ff0000000000000 give -1 in byte or short but the max value in other types?
    – Lino1000
    Nov 20 '18 at 14:43










  • Sorry, my bad, I somehow read 0 instead of -1 :/
    – m0skit0
    Nov 20 '18 at 14:46


















  • sorry if it might sound stupid to you or something but why does 0x7ff0000000000000 give -1 in byte or short but the max value in other types?
    – Lino1000
    Nov 20 '18 at 14:43










  • Sorry, my bad, I somehow read 0 instead of -1 :/
    – m0skit0
    Nov 20 '18 at 14:46
















sorry if it might sound stupid to you or something but why does 0x7ff0000000000000 give -1 in byte or short but the max value in other types?
– Lino1000
Nov 20 '18 at 14:43




sorry if it might sound stupid to you or something but why does 0x7ff0000000000000 give -1 in byte or short but the max value in other types?
– Lino1000
Nov 20 '18 at 14:43












Sorry, my bad, I somehow read 0 instead of -1 :/
– m0skit0
Nov 20 '18 at 14:46




Sorry, my bad, I somehow read 0 instead of -1 :/
– m0skit0
Nov 20 '18 at 14:46



Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?