- 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"...
Originally posted by: singh
Why does the compiler report an "error" instead of a warning? Anyone care to postulate?
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.
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.
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!
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.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.
