Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

loss of precision

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wanted to know if there is a way to get around this error by the compiler. I have two lines of code where this error is happening.
Variables (theoctet) and (remainder) are integers:

theoctet = 256 - Math.pow(2, remainder);
intOctet[whole] = theoctet;

Based on the possible values I could use, the value of theoctet will never exceed the integer threshold. The problem is if I change 'theoctet' to a double I'm also going to have to change every other array or variable I have later on in code that uses this method. I really don't want to do that especially if I don't need it. Is there anyway around this error? Maybe a try can be used to throw and exception if the result exceeds the value?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tyler,

The way to tell the compiler "I know, but it's OK" is to use a cast. This will give you a "posible loss of precision" type of error:

int x = 0;
double d = 1;
x = d;

Now, you say to yourself "That's silly, I know d is 1, and so this is going to be just fine". To tell the compiler that it's going to be fine, you "cast d to an int:"

x = (int) d;

d is then converted to an int before the assignment is made. Any actual loss of precision that occurs (i.e., overflow) is ignored.

Casts are useful in many other contexts as well -- maybe you've already used them with objects, but not with primitives in this way.
 
Tyler Jordan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.
Is there any way to get rid of the .0 at the end of the value now. The problem is that I put it in a string and unfortunately the cast carries over the .0 into the integer.
 
Tyler Jordan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Figured it out, I didn't change the array back to integer, so it was maintaining the double format. That was my bust. Thanks for the information about the cast. Works appropriately.
 
It's never done THAT before. Explain it to me tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic