0

I just noticed that my expressions such as

double x = 60/21

returns incorrect values.

I found out that this is because 60 and 21 are integers. I should be using

double x = 60.0/21.0

Is there a way to work around the need for adding the .0 to every integer to get accurate results?

Basically I don't want to add the decimal point to every integer in my code. I want double x = 60/21 to always be treated as double x = 60.0/21.0

Is there any way to do this?

UPDATE: Thanks for the great answers

Supercreature
  • 441
  • 6
  • 25
  • 2
    No, but you only have to add .0 to one of the ints anyway. – cpp beginner Apr 03 '18 at 13:48
  • 1
    Each time you divide, cast one or both of the numerator or denominator to double. – StuartLC Apr 03 '18 at 13:48
  • 2
    Possible duplicate of [Division of integers in Java](https://stackoverflow.com/questions/7220681/division-of-integers-in-java) – kabanus Apr 03 '18 at 13:49
  • 1
    Retracted the close vote. You can make your own division function. Doesn't seem like it would save space though. – kabanus Apr 03 '18 at 13:50
  • 1
    You only need to either implicitly (add `.0`) or explicitly (append `d` or `D`) use `double` for one term of each expression. You *will*, however, need those changes, as an integer literal is interpreted as `int` by default in Java, hence an expression involving two `int`s will return `int`. – Mena Apr 03 '18 at 13:51

4 Answers4

1

No, there's not really a better way. (You could add a cast, but that's not really different than adding a .0 to the end of a literal value). Computers do exactly what you tell them to do, so you have to be specific.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
1

How to treat all integers in expressions as doubles? Is there any way to do this?

Short answer: no there isn't. The Java language provides no mechanism that can alter the language's innate rules for converting values in expressions.

If you want your Java expressions to use double arithmetic for integral operands, you need to explicitly cast them.

The closest you could come to what you want would be to write a preprocessor that analyses your Java source code and inserted casts at the appropriate point. (Or maybe, a tool that analyzed your code for examples of "harmful" integer arithmetic.) But developing such a tool would be orders of magnitude more work than just learning to be careful in the way you write expressions. (IMO)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

In a real program, you wouldn't have 60 or 21 hard coded, but they'd be passed in as arguments or would be a local variable of some sort.

To avoid having to add the .0 to the end, you can just declare the type of these numbers to be doubles rather than integers. This will cause division to return a double as well.

Chris S.
  • 192
  • 3
  • This is an overly broad claim. Plenty of real programs involve division of two literals. For example, plenty of real programs need to use the value `0.5`, and it's easy to accidentally write that as `1/2`. – user2357112 Apr 04 '18 at 18:11
  • When you use 'magic numbers' like this, best practices would call for defining them with a local variable or constant, where you could declare it as a double. If you're going to sprinkle 0.5 and 1/2 all over your code it's going to be very buggy if you later want to use .4 and 2/5's in some places and then knowing which places to replace and not. – Chris S. Apr 05 '18 at 18:18
  • `double whatever = 1/2;` is still 0. – user2357112 Apr 05 '18 at 18:21
0

Cast your integer to double, like:

int num1 = 60;
int num2 = 21;
double x = (double) num1 / (double) num2;