Possible Loss of Precision - not caused by type conversion - Java
This is a discussion on Possible Loss of Precision - not caused by type conversion - Java ; Hope I will not get banned for too many posts
The code below does not want to compile. I get ""Possible Loss of Precision"
in the lines:
'j=last;'
'sito[j]=i;'
'increase(i,last);'
Whats wrong with it ?
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
package narzedzia;
public ...
-
Possible Loss of Precision - not caused by type conversion
Hope I will not get banned for too many posts 
The code below does not want to compile. I get ""Possible Loss of Precision"
in the lines:
'j=last;'
'sito[j]=i;'
'increase(i,last);'
Whats wrong with it ?
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
package narzedzia;
public class LiczbyPierwsze {
protected final static long length = 21;
protected final static long sito [] = new long [(long)1<<length];
// inicjalizacja sita :
{
sito[0]=1;
sito[1]=1;
long last = 0L;
long j;
long i =2;
while(i<length){ // in i we got the following prime
j=last;
while(j<length){ // we mark the numbers whose smallest divisor is i
if(sito[j]== j);
sito[j]=i;
j+=i;
}
increase(i,last); // we count the next prime for the sive
}
}
public final static long czyPierwsza(long x ){
;
return 0;
}
public final static long naCzynnikiPierwsze(long x){
;
return 0;
}
private final static void increase(long x, long last){
while(sito[last++]!=(long)0);
x =(long)sito[last];
}
}
-
Re: Possible Loss of Precision - not caused by type conversion
Thomas wrote:
> Hope I will not get banned for too many posts 
> The code below does not want to compile. I get ""Possible Loss of Precision"
> in the lines:
> long last = 0L;
> long j;
> long i =2;
....
> 'j=last;'
>
> 'sito[j]=i;'
Maybe because
> Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion (§) and become int values. An attempt to access an array component with a long index value results in a compile-time error.
<http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.4>
> 'increase(i,last);'
The value of i will not change from this call - you knew that, yes?
> private final static void increase(long x, long last){
> while(sito[last++]!=(long)0);
> x =(long)sito[last];
> }
The value of x from the assignment is thrown away.
--
Lew
-
-
Re: Possible Loss of Precision - not caused by type conversion
Thomas wrote:
> Hope I will not get banned for too many posts 
> The code below does not want to compile. I get ""Possible Loss of Precision"
> in the lines:
> 'j=last;'
....
I don't understand that particular error, and Eclipse did not report it.
j and last are both of type long, so the assignment should be valid.
There is an error on each attempt to use a long as an array index. That
is due to the unfortunate decision to limit Java arrays by using int for
array size and index.
Patricia
-
Re: Possible Loss of Precision - not caused by type conversion
"Lew" <lew@lewscanon.nospam> napisa� w wiadomo�ci
>> Maybe because
>>> Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion (�) and become int values. An attempt to access an array component with a long index value results in a compile-time error.
>> <http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.4>
Thomas wrote:
> Now after changing from long to int it works. Java sux if it doesn't allow
> on more flexibilty. THX
If you have an array of long with 2 billion entries it will occupy over 16GB
of heap - the issue of your index will be moot.
Since arrays are limited to (Integer.MAX_VALUE + 1) entries, using a long
index risks an out-of-bounds problem, hence the restriction to int indexes.
You used a long, which in conversion to int risks losing precision, and this
is the error you got. In today's world an array with more entries than that
is not manageable anyway.
You can always downcast the long index to int for use in the indexing expression.
I do not begin to guess how you can characterize this as an issue of
"flexibility".
--
Lew
-
Re: Possible Loss of Precision - not caused by type conversion
On Jul 7, 9:44 pm, Lew <l...@lewscanon.nospam> wrote:
> "Lew" <l...@lewscanon.nospam> napisa? w wiadomo?ci
> I do not begin to guess how you can characterize this as an issue of
> "flexibility".
I agree. Plus no one 'forces' anyone to use arrays. You can always use
'Arraylist' if you want _flexibility_.
-
Re: Possible Loss of Precision - not caused by type conversion
getsanjay.sharma wrote:
> On Jul 7, 9:44 pm, Lew <l...@lewscanon.nospam> wrote:
>> "Lew" <l...@lewscanon.nospam> napisa? w wiadomo?ci
>> I do not begin to guess how you can characterize this as an issue of
>> "flexibility".
>
> I agree. Plus no one 'forces' anyone to use arrays. You can always use
> 'Arraylist' if you want _flexibility_.
Although it lacks the particular feature the OP wanted, the ability to index
outside the int range.
--
Lew
-
Re: Possible Loss of Precision - not caused by type conversion
On Jul 7, 10:55 pm, Lew <l...@lewscanon.nospam> wrote:
> Although it lacks the particular feature the OP wanted, the ability to index
> outside the int range.
I know it sounds expensive memory wise but OP can always use the Hash
Map and with the autoboxing capability, he can index outside the int
range.
-
Re: Possible Loss of Precision - not caused by type conversion
On Jul 7, 12:44 pm, Lew <l...@lewscanon.nospam> wrote:
> If you have an array of long with 2 billion entries it will occupy over 16GB
> of heap - the issue of your index will be moot.
Yeah, since 640K really ought to be enough for anybody, even far into
the future ... 
-
Re: Possible Loss of Precision - not caused by type conversion
Lew wrote:
> "Lew" <lew@lewscanon.nospam> napisa� w wiadomo�ci
>>> Maybe because
>>>> Arrays must be indexed by int values; short, byte, or char values
>>>> may also be used as index values because they are subjected to unary
>>>> numeric promotion (�) and become int values. An attempt to access an
>>>> array component with a long index value results in a compile-time
>>>> error.
>>> <http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.4>
>
> Thomas wrote:
>> Now after changing from long to int it works. Java sux if it doesn't
>> allow
>> on more flexibilty. THX
>
> If you have an array of long with 2 billion entries it will occupy over
> 16GB of heap - the issue of your index will be moot.
Not if you have a 64 bit JVM and a server with a large memory.
Patricia
Similar Threads
-
By Application Development in forum c++
Replies: 13
Last Post: 12-04-2007, 08:39 PM
-
By Application Development in forum Idl-pvwave
Replies: 2
Last Post: 05-17-2007, 09:07 AM
-
By Application Development in forum c++
Replies: 18
Last Post: 11-11-2006, 05:56 PM
-
By Application Development in forum awk
Replies: 4
Last Post: 06-20-2006, 04:39 PM
-
By Application Development in forum Graphics
Replies: 0
Last Post: 10-23-2003, 01:59 PM