You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Kevin Jackson <fo...@gmail.com> on 2006/09/26 05:39:24 UTC

[math][patch] Mantissa + Characteristic methods for MathUtils

Hi,

I've just been working on some fairly simple code (in any other
language), and realised that I needed some of the most basic methods
available and suprise Java doesn't have them in the base libraries.

These are very tiny, but I can't believe that no-one else has ever
needed to get the mantissa of a float/double before me - if these
exist in some other form please forgive this intrusion.

Adding these would greatly simplify my current code, or if anyone
knows how to get the mantissa without resorting to String manipulation
- even using x * 10^mantissaLength % 10^mantissaLength /
10^mantissaLength is fairly nasty in client code (and you get rounding
errors that way).

Thanks,
Kev


Re: [math][patch] Mantissa + Characteristic methods for MathUtils

Posted by Luc Maisonobe <Lu...@free.fr>.
Kevin Jackson a écrit :

> Adding these would greatly simplify my current code, or if anyone
> knows how to get the mantissa without resorting to String manipulation
> - even using x * 10^mantissaLength % 10^mantissaLength /
> 10^mantissaLength is fairly nasty in client code (and you get rounding
> errors that way).

Here is some code from the 
org.apache.commons.math.util.MathUtils.nextAfter method:

         // split the double in raw components
         long bits     = Double.doubleToLongBits(d);
         long sign     = bits & 0x8000000000000000L;
         long exponent = bits & 0x7ff0000000000000L;
         long mantissa = bits & 0x000fffffffffffffL;

This snippet cuold be promoted to public methods by itself (beware that 
the exponent part is the raw exponent which includes an offset, it is 
not a simple integer).

Luc

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [math][patch] Mantissa + Characteristic methods for MathUtils

Posted by Kevin Jackson <fo...@gmail.com>.
> Best I've found on the net uses some bit arithmetic.
>
> long bits = Double.doubleToLongBits(5894.349580349);
>
> boolean negative = (bits & 0x8000000000000000L) != 0;
> long exponent = bits & 0x7ff0000000000000L >> 52;
> long mantissa = bits & 0x000fffffffffffffL;

Yep, that's about what I got from the Java5 API.  It's awfully obtuse,
and thoroughly non-intuitive.

Oh well,

Kev

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [math][patch] Mantissa + Characteristic methods for MathUtils

Posted by Mark Diggory <md...@gmail.com>.
Best I've found on the net uses some bit arithmetic.

long bits = Double.doubleToLongBits(5894.349580349);

boolean negative = (bits & 0x8000000000000000L) != 0;
long exponent = bits & 0x7ff0000000000000L >> 52;
long mantissa = bits & 0x000fffffffffffffL;

Cheers,
Mark

On Sep 25, 2006, at 11:39 PM, Kevin Jackson wrote:

> Hi,
>
> I've just been working on some fairly simple code (in any other
> language), and realised that I needed some of the most basic methods
> available and suprise Java doesn't have them in the base libraries.
>
> These are very tiny, but I can't believe that no-one else has ever
> needed to get the mantissa of a float/double before me - if these
> exist in some other form please forgive this intrusion.
>
> Adding these would greatly simplify my current code, or if anyone
> knows how to get the mantissa without resorting to String manipulation
> - even using x * 10^mantissaLength % 10^mantissaLength /
> 10^mantissaLength is fairly nasty in client code (and you get rounding
> errors that way).
>
> Thanks,
> Kev
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org