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 / General / November 2005

Tip: Looking for answers? Try searching our database.

Newbie: Error in mod evaluation

Thread view: 
juneappal - 02 Nov 2005 06:18 GMT
Sorry to pester the list with this - it is such a simple problem I
can't figure out what to search for.

This is m first day of playing with Java.  I notice that

> System.out.println((34/5.0)%1);

evaluates as:

0.7999999999999998

Why is that?
Andrew Thompson - 02 Nov 2005 06:44 GMT
> Sorry to pester the list ..

usenet newsgroup (specifically).  A 'list' generally refers to
a mailing-list, which this is not.

>..with this - it is such a simple problem I
> can't figure out what to search for.
>
> This is m first day of playing with Java.  

I think you'll have a lot of fun.  Quick note.  A better
group for those just starting in Java/programming is
<http://www.physci.org/codes/javafaq.jsp#cljh>

Though it makes good sense to continue *reading* c.l.j.programmer.

>>System.out.println((34/5.0)%1);
...
> 0.7999999999999998

The machine representation of floating point numbers is
inherently inaccurate.  It is the saem for all programming
languages.  The simple fix for Java is to apply a
NumberFormat or DecimalFormat to the output.
<http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html>
<http://java.sun.com/j2se/1.5.0/docs/api/java/text/NumberFormat.html>

HTH
Roedy Green - 02 Nov 2005 07:11 GMT
>This is m first day of playing with Java.  I notice that
>
[quoted text clipped - 5 lines]
>
>Why is that?

See http://mindprod.com/jgloss/floatingpoint.html

Are you sure you meant %?? % is an integer operator.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 02 Nov 2005 07:18 GMT
On Wed, 02 Nov 2005 06:11:09 GMT, Roedy Green
<my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or
indirectly quoted someone who said :

>Are you sure you meant %?? % is an integer operator.

% is defined for floating point but I think every production use I
have ever seen of it was on ints.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Thomas Schodt - 02 Nov 2005 11:45 GMT
> % is defined for floating point

As described here
<http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#239829>
Roedy Green - 02 Nov 2005 12:06 GMT
On Wed, 02 Nov 2005 10:45:52 +0000, Thomas Schodt
<spamtrap@xenoc.demon.co.uk.invalid> wrote, quoted or indirectly
quoted someone who said :

>> % is defined for floating point
>
>As described here
><http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#239829>

quoting from the Java glossary which in turn quotes from the JLS:
http://mindprod.com/jgloss/modulus.html

% is also defined to work with float and double operands, though that
use is quite rare.

The result of a floating-point remainder operation as computed by the
% operator is not the same as that produced by the remainder operation
defined by the IEEE 754 floating point standard. The IEEE 754
remainder operation computes the remainder from a rounding division,
not a truncating division. % on floating-point operations behaves
analogously to the integer remainder operator; this may be compared
with the C library function fmod. The IEEE 754 remainder operation may
be computed by the library routine Math. IEEEremainder. (Note the
violation of the naming conventions.)

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



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



©2009 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.