1

I have very big numbers and complex string equations to solve in java. For that I use BeanShell. In this equations can also contain bitwise binary operations e.g.

 (log(100)*7-9) & (30/3-7)

should be 1. As I said I need to handle huge numbers for that I add the L to each number which works fine so far. But here I have the problem when computing something like 3/2 I just receive 1 and not 1.5. Then I've tried to add a D for double values to each number which gives me the 1.5 but here I receive an error on the binary operations and or xor etc. because of course they can only be applied to Integer values.

Is there a way to receive double values when needed and still perform the binary operations (of course only when I have integer values)?

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
wasp256
  • 5,943
  • 12
  • 72
  • 119

1 Answers1

0

I would declare the operands as doubles at the outset. It seems to me leaving Beanshell to promote/demote based on the answer introduces uncertainty into your code.

You can cast the double to an int and perform the binary operation every time. As this is a narrowing primitive conversion you should familiar yourself with the Java Language Specs.

(Another possibly helpful answer)

Community
  • 1
  • 1
GaryMcM
  • 425
  • 3
  • 9
  • When I always cast the right and left site of the binary op to an int it will work, but I shouldn't because when I have something like `((5-2)/2) and ((3-2)/2) -> 1.5 and 0.5` I won't to receive an error without simply cutting of the decimal part... Is there probably an alternative to BeanShell for solving such equations? – wasp256 Oct 09 '13 at 18:19
  • Why not write a function that tells you it's an integer? Example: `isInt(i) { return Math.floor(i) == i; }` – GaryMcM Oct 10 '13 at 01:20