954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

separate mantissa and exponent of a double

hi every one ....

thanks for comming to this thread ...

Dose any one know how to separate mantissa and exponent of a double and store them in two integers? :?:

hasan2003
Newbie Poster
8 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

I'm not sure if this is exactly what you're talking about, but here it is...

public class Mantissa
{
	public static void main(String[] args)
	{
		double d = 2.88;
		int a = (int) d; // here's the first part
		System.out.println(a);
		double f = d - (double) a;
		f = Math.round(f * 100);
		int c = (int) f;
		System.out.println(c);
	}
}
stupidenator
Junior Poster
192 posts since Mar 2005
Reputation Points: 18
Solved Threads: 4
 

I'm not sure if this is exactly what you're talking about, but here it is...

public class Mantissa
{
	public static void main(String[] args)
	{
		double d = 2.88;
		int a = (int) d; // here's the first part
		System.out.println(a);
		double f = d - (double) a;
		f = Math.round(f * 100);
		int c = (int) f;
		System.out.println(c);
	}
}


yes, but is there any other way? or is there any chance of data lose in this way?

hasan2003
Newbie Poster
8 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 
yes, but is there any other way? or is there any chance of data lose in this way?

I thought that was a very good solution myself, but if you want another way, you could throw it into a string and parse it.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

hi every one ....

thanks for comming to this thread ...

Dose any one know how to separate mantissa and exponent of a double and store them in two integers? :?:


I'd go with something like

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



Note that the mantissa is actually 52 bits in a double, and shouldn't ever be put in an integer without making sure it fits first. The exponent is small enough.

Hope that helps.

Melldrin
Newbie Poster
1 post since Aug 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You