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.

Fixed-point format for floating-point numbers

Thread view: 
Motaz Saad - 03 Nov 2005 23:52 GMT
Dear All,
Please how can I fix the significant places (number of digits after the
float point) for the floating-point numbers in java?
Thanks In Advance.
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Motaz Khalid Saad.
Department Of Computer Science
Information Technology College
Islamic University - Gaza - Palestine
http://www.iugaza.edu.ps
http://www.geocities.com/motsad
e-mail: motaz.saad@gmail.com , motsad@yahoo.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Roedy Green - 04 Nov 2005 00:08 GMT
>Please how can I fix the significant places (number of digits after the
>float point) for the floating-point numbers in java?
>Thanks In Advance.

Reframing your question, how do I convert a double to a String?

see http://mindprod.com/applets/converter.html
Signature

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

Joan - 04 Nov 2005 00:19 GMT
> On 3 Nov 2005 14:52:52 -0800, "Motaz Saad"
> <motaz.saad@gmail.com>
[quoted text clipped - 8 lines]
>
> see http://mindprod.com/applets/converter.html

Not to be picky, but on current hardware there is no "float
point"
rather it is assumed to be at the left of the mantissa bits.
Andrew Thompson - 04 Nov 2005 00:21 GMT
> Please how can I fix the significant places (number of digits after the
> float point) for the floating-point numbers in java?

Sounds as if you are after either,
<http://java.sun.com/j2se/1.5.0/docs/api/java/text/NumberFormat.html>
..or..
<http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html>
Tim Slattery - 04 Nov 2005 14:58 GMT
>Dear All,
>Please how can I fix the significant places (number of digits after the
>float point) for the floating-point numbers in java?
>Thanks In Advance.

You can't, that's why they are called floating point numbers.

If you need to do fixed point arithmetic, look at the BigDecimal
class.

--  
Tim Slattery
Slattery_T@bls.gov
Tex - 04 Nov 2005 15:53 GMT
> Please how can I fix the significant places (number of digits after the
> float point) for the floating-point numbers in java?
> Thanks In Advance.

Floating point in any language and on any hardware is not suitable for
fixing significant digits.  This question frequently comes up for science
& engineering wherein there are rules for the number of significant
digits you should allow for computations, i.e. how accurate your can
express your results.

Floating point does not contain all decimal fractions, e.g. 0.10 does not
exist in floating point.  A close binary representation (< 0.10) will be
stored.
It is kinda like s = 1/3 actually stores 0.333333 so 1/3 is not stored.  So
when you enter an arbitrary fractional number, say d = 32.5694, that
number may well not exist, so only an approximation is stored, and
similarly for interiem computational results.

Mostly results are close enough for engineering & gov't work to just
round and convert to a string for people.  When exact control of precision
is required you need to work in integer arithmetic, i.e. many financial
calculations are done in 1/100 of a cent, because banks don't want to
lose or gain pennies every now and then, which would result if they used
floating point to keep our bank records.  Physics frequently requires
great precision and control of significant results and thus integer
arithmetic.  To a mathematician, floating point doesn't work at all.

You can get (free) math packages to handle very large numbers of
significant digits if your numbers cannot be handled by standard
integers.

--tex
Tim Slattery - 04 Nov 2005 18:45 GMT
>Floating point does not contain all decimal fractions, e.g. 0.10 does not
>exist in floating point.  A close binary representation (< 0.10) will be
>stored.

<NITPICK>
Yes, but that's not because it's "floating point", it's because it's
*binary* floating point.
</NITPICK>

--  
Tim Slattery
Slattery_T@bls.gov
Joan - 05 Nov 2005 18:33 GMT
>>Floating point does not contain all decimal fractions, e.g.
>>0.10 does not
[quoted text clipped - 7 lines]
> *binary* floating point.
> </NITPICK>

I think it is interesting that I wrote a JNI program in C that
returns the
number of bytes on a MS Windows machine hard drive as a float.
(And it works.)


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.