Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2007

Tip: Looking for answers? Try searching our database.

altenatives for double is mathematical integer

Thread view: 
Jeff Higgins - 26 Jan 2007 02:51 GMT
Hi,
Can anyone show me alteratives to the
following for finding if a given Java
double is an mathematical integer?

Thanks,
Jeff Higgins

public class Test_Increment {
 public static void main(String[] args) {
   double d = Double.parseDouble(args[0]);
   System.out.println((d-Math.ceil(d))==0);
 }
}
Ingo R. Homann - 26 Jan 2007 08:31 GMT
Hi,

> Hi,
>  Can anyone show me alteratives to the
[quoted text clipped - 10 lines]
>   }
> }

Well, to answer your question, there might be other solutions like:

d==(int)d

You could encapsulate/refactor this test to a "utility method" and not
care about *how* the test works exactly.

But your question shows that you might have a more general problem in
your application. Ever tried something like this?:

      double d = 0.1;
      double sum = 0;
      for (int i = 0; i < 10; i++)
      {
        sum = sum + d;
      }
      System.out.println(sum);
      System.out.println(sum == 1.0); // false!!!

Perhaps, the class "BigDecimal" might be a good idea for you!

Ciao,
Ingo
Patricia Shanahan - 26 Jan 2007 21:46 GMT
> Hi,
>
[quoted text clipped - 16 lines]
>
> d==(int)d

That answers a different question, whether the double is exactly equal
to some int. It will reject many doubles that are mathematical integers,
but outside the int range.

The original question has several subtle issues, such as making sure the
infinities are rejected, and the posted code looks correct to me. Why
look for alternatives?

Patricia
Stefan Z Camilleri - 26 Jan 2007 21:54 GMT
>> Hi,
>>> Hi,
[quoted text clipped - 23 lines]
>
> Patricia

I think the simplest and most effective way around this is simply...

System.out.println( 0 == d % 1 ? "Is Integer" : "Is Double" );

Signature

- Stefan Z Camilleri
- www.szc001.com

Jeff Higgins - 29 Jan 2007 01:04 GMT
> The original question has several subtle issues, such as making sure the
> infinities are rejected, and the posted code looks correct to me. Why
> look for alternatives?

I probably wasn't looking for alternatives
as much as hoping to understand the issues.
I hadn't accounted for infinities and NAN plus further
testing showed that d-Math.ceil(d))==0 doesn't work
for at least Double.MIN_VALUE.
Thanks. JH

> Patricia
Jeff Higgins - 29 Jan 2007 00:43 GMT
> Hi,
> Can anyone show me alteratives to the
[quoted text clipped - 10 lines]
>  }
> }

Thanks Ingo,Patricia,Stefan.
Appreciate the response.

public class Test {
 public static void main(String[] args) {

 double zero = 0.0;
 double one = 1.0;
 double nOne = -1.0;
 double pInt = Integer.MAX_VALUE + 1;
 double nInt = Integer.MIN_VALUE - 1;
 double pDbl = Integer.MAX_VALUE + 1024.3;
 double nDbl = Integer.MIN_VALUE - 1024.3;
 double nan = Double.NaN;
 double pInf = Double.POSITIVE_INFINITY;
 double nInf = Double.NEGATIVE_INFINITY;
 double max = Double.MAX_VALUE;
 double min = Double.MIN_VALUE;
 double nrml = Double.MIN_NORMAL;

 double[] a = {zero,one,nOne,pInt,nInt,pDbl,
     nDbl,nan,pInf,nInf,max,min,nrml};

 System.out.println("//d-Math.ceil(d)==0");
 for(int i=0;i<a.length;i++){
   if(a[i]!=0)
   System.out.print((a[i]-Math.ceil(a[i]))==0);
   System.out.println(" " + Double.valueOf(a[i]).toString());
 }
 System.out.println("\n//d==(int)d");
 for(int i=0;i<a.length;i++){
   if(a[i]!=0)
   System.out.print(a[i]==(int)a[i]);
   System.out.println(" " + Double.valueOf(a[i]).toString());
 }
 System.out.println("\n//0==d%1");
   for(int i=0;i<a.length;i++){
     if(a[i]!=0)
     System.out.print(0 == a[1] % 1 );
     System.out.println(" " + Double.valueOf(a[i]).toString());
   }
 }
}

//d-Math.ceil(d)==0
0.0
true 1.0
true -1.0
true -2.147483648E9
true 2.147483647E9
false 2.1474846713E9
false -2.1474846723E9
false NaN
false Infinity
false -Infinity
true 1.7976931348623157E308
false 4.9E-324
false 2.2250738585072014E-308

//d==(int)d
0.0
true 1.0
true -1.0
true -2.147483648E9
true 2.147483647E9
false 2.1474846713E9
false -2.1474846723E9
false NaN
false Infinity
false -Infinity
false 1.7976931348623157E308
false 4.9E-324
false 2.2250738585072014E-308

//0==d%1
0.0
true 1.0
true -1.0
true -2.147483648E9
true 2.147483647E9
true 2.1474846713E9
true -2.1474846723E9
true NaN
true Infinity
true -Infinity
true 1.7976931348623157E308
true 4.9E-324
true 2.2250738585072014E-308
Jeff Higgins - 29 Jan 2007 03:44 GMT
Correction:

I botched the above test.
Sorry, especially Stefan.

System.out.print(0==a[1]%1
Should be:
System.out.print(0==a[i]%1

Thanks.
Jeff Higgins

public class Test {
 public static void main(String[] args) {

 double zero = 0.0;
 double one = 1.0;
 double nOne = -1.0;
 double pInt = Integer.MAX_VALUE + 1;
 double nInt = Integer.MIN_VALUE - 1;
 double pDbl = Integer.MAX_VALUE + 1024.3;
 double nDbl = Integer.MIN_VALUE - 1024.3;
 double nan = Double.NaN;
 double pInf = Double.POSITIVE_INFINITY;
 double nInf = Double.NEGATIVE_INFINITY;
 double max = Double.MAX_VALUE;
 double min = Double.MIN_VALUE;
 double nrml = Double.MIN_NORMAL;

 double[] a = {zero,one,nOne,pInt,nInt,pDbl,
     nDbl,nan,pInf,nInf,max,min,nrml};

 System.out.println("//d-Math.ceil(d)==0");
 for(int i=0;i<a.length;i++){
   if(a[i]!=0)
   System.out.print((a[i]-Math.ceil(a[i]))==0);
   System.out.println(" " + Double.valueOf(a[i]).toString());
 }
 System.out.println("\n//d==(int)d");
 for(int i=0;i<a.length;i++){
   if(a[i]!=0)
   System.out.print(a[i]==(int)a[i]);
   System.out.println(" " + Double.valueOf(a[i]).toString());
 }
 System.out.println("\n//0==d%1");
   for(int i=0;i<a.length;i++){
     if(a[i]!=0)
     System.out.print(0 == a[i] % 1 );
     System.out.println(" " + Double.valueOf(a[i]).toString());
   }
 }
}

//d-Math.ceil(d)==0
0.0
true 1.0
true -1.0
true -2.147483648E9
true 2.147483647E9
false 2.1474846713E9
false -2.1474846723E9
false NaN
false Infinity
false -Infinity
true 1.7976931348623157E308
false 4.9E-324
false 2.2250738585072014E-308

//d==(int)d
0.0
true 1.0
true -1.0
true -2.147483648E9
true 2.147483647E9
false 2.1474846713E9
false -2.1474846723E9
false NaN
false Infinity
false -Infinity
false 1.7976931348623157E308
false 4.9E-324
false 2.2250738585072014E-308

//0==d%1
0.0
true 1.0
true -1.0
true -2.147483648E9
true 2.147483647E9
false 2.1474846713E9
false -2.1474846723E9
false NaN
false Infinity
false -Infinity
true 1.7976931348623157E308
false 4.9E-324
false 2.2250738585072014E-308


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.