? about JAVA mathematics

Jul 12, 2001
10,142
2
0
i am using nubers in the Short class and want to do math operations, but whenever I try to do them i recieve an error "possible loss of precision"...
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
are you mixing types, or using non-integer values anywhere? For example
foo = bar / 2.5
even if foo, bar are both short, bar has to be converted to float or double for the division, then converted back to short with, surprise, possible loss of precision.
 

wfay

Senior member
Jul 24, 2001
912
0
0
pls post sample code with comments showing where it breaks, and sample inputs, and we can provide better assistance... :)
 

damiano

Platinum Member
May 29, 2002
2,322
1
0
start using native double.... haha

why are you using short affraid of running out of memory?

The whole thing is that short uses less memory, so the precision is less.
think about it...
and unless you machine has 5 megs of ram or your system has a LOT of transactions... just use double
 
Jul 12, 2001
10,142
2
0
im using short because it is what is returned by a program not written by me.

i am trying to subtract two shorts or multimply a short by an int...as follows

temp[x][y] = 4*temparray[x][y]-temparray[x+1][y]-temparray[x-1][y]-temparray[x][y+1]-temparray[x][y-1];

temp and temparray are short[][]
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Why does the compiler report an "error" instead of a warning? Anyone care to postulate?
 

manly

Lifer
Jan 25, 2000
13,579
4,236
136
Basically, numeric types get promoted to a wider type when you do mixed-type arithmetic. While the rules involved are not trivial, in general you can simply assume numeric math is performed with ints and floating point math with doubles.

So if you're manipulating ints and want to store the result into a short, then you'll have to cast (narrow) the type to short.
 
Jul 12, 2001
10,142
2
0
Originally posted by: manly
Basically, numeric types get promoted to a wider type when you do mixed-type arithmetic. While the rules involved are not trivial, in general you can simply assume numeric math is performed with ints and floating point math with doubles.

So if you're manipulating ints and want to store the result into a short, then you'll have to cast (narrow) the type to short.

thanks thats getting me on the right track...for some reason i still get the error when i put (short) at the start of the operation...just keeps telling me i might be losing precision...and im just sitting here yelling at the screen that i know and that i dont mind...but that doesnt help

rolleye.gif
 

manly

Lifer
Jan 25, 2000
13,579
4,236
136
Just to be clear, did you do did exactly this:

temp[x][y] = (short) (4*temparray[x][y]-temparray[x+1][y]-temparray[x-1][y]-temparray[x][y+1]-temparray[x][y-1]);

A narrowing cast might issue a compiler warning, but it doesn't raise an error. Also, I don't see why you can't be using int or int[][] even though you feel otherwise.
 
Jul 12, 2001
10,142
2
0
Originally posted by: manly
Just to be clear, did you do did exactly this:

temp[x][y] = (short) (4*temparray[x][y]-temparray[x+1][y]-temparray[x-1][y]-temparray[x][y+1]-temparray[x][y-1]);

A narrowing cast might issue a compiler warning, but it doesn't raise an error. Also, I don't see why you can't be using int or int[][] even though you feel otherwise.

i hate programming...didnt have the () around the statement..just the short...

thanx!
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: MrDingleDangle
Originally posted by: manly
Just to be clear, did you do did exactly this:

temp[x][y] = (short) (4*temparray[x][y]-temparray[x+1][y]-temparray[x-1][y]-temparray[x][y+1]-temparray[x][y-1]);

A narrowing cast might issue a compiler warning, but it doesn't raise an error. Also, I don't see why you can't be using int or int[][] even though you feel otherwise.

i hate programming...didnt have the () around the statement..just the short...

thanx!

when you put the the parens around it its called a type cast, you are basically telling the compiler this is the type of the variable coming next, if you dont have the parens you are beggining a declaration.
 

manly

Lifer
Jan 25, 2000
13,579
4,236
136
Originally posted by: Ameesh

when you put the the parens around it its called a type cast, you are basically telling the compiler this is the type of the variable coming next, if you dont have the parens you are beggining a declaration.
I'm pretty sure he means that he didn't put parens around the expression, thus the (short) cast applied only to the first term of the expression (namely 4), instead of to the evaluation of the whole expression.