The Artima Developer Community
Sponsored Link

Java Answers Forum
Why is this a loss of precision error?

1 reply on 1 page. Most recent reply: Jun 11, 2010 5:09 AM by Vincent O'Sullivan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
See Furst

Posts: 1
Nickname: cheruvim
Registered: Jun, 2010

Why is this a loss of precision error? Posted: Jun 10, 2010 9:51 PM
Reply to this message Reply
Advertisement
Ok Here's one thing that really irks me about Java, It's treatment of literals. Granted it's all Objects and Collections of Objects really but why can't you do this:

byte b = 0xff & 0xff; // loss of precision?? @#$56@#$%#$^345

you can do this:

byte b = 0x0f & 0x0f //? 15 - 0000 1111 -


According to comp-sci 101 class the first statement should not result in loss of precision because a byte is a byte and 0xff is 1111 1111 which is, you guessed it, a byte.

bit-and it with another set of 1111 1111 should result in 1111 1111 which doesn't looks like precision loss of anything.

You can answer, "twos complement", but two's complement is for representing negative and positive INTEGERS. It is implied when using hex that these are going to represent the INTEGERS in two's compliment so saying it's a loss of precision is false because you aren't actually loosing anything. Just a bad compiler, really.


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Why is this a loss of precision error? Posted: Jun 11, 2010 5:09 AM
Reply to this message Reply
0xff = 255 (not -127).

You're assumption is wrong that use of two's complement is implied when using hex (unless you can provide documentation that implies otherwise).

Java bytes have an integer range of -127 to +128, therefore 0xff is outside the permitted range, hence the loss of precision. The compiler is working correctly.

Flat View: This topic has 1 reply on 1 page
Topic: plz help me........... Previous Topic   Next Topic Topic: Insert one DefaultStyledDocument into another DefaultStyledDocument

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use