473,395 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

storing a long in a float with same precision on Solaris 64bit OS

On Solaris 64-bit:

sizeof(long) = 8
sizeof(double) = 8 !!!

So i figure i can't store a long with the same precision in a double.
But, when i create a long and assign its largest value to it, then
assign this to a double, then subtract the double from the long, I get
0.

I see from the include files the exponent is 11 bits. So the Mantissa
must be 53 (or less) bits. So how can the double store my long to the
same precision??

I've posted the code and output below.

Any help appreciated.

Nishant

------------------------------
#include <stdlib.h>
#include <iostream.h>
#include <stdio.h>

int main()
{
cout << sizeof(long) << endl;
cout << sizeof(double) << endl;
cout << sizeof(float) << endl;

long l = 0x7FFFFFFFFFFFFFFF;
//long l = 9223372036854775807LL;
cout << "l = " << l << endl;

// want to confirm l is indeed the largest long
long nl = l + 1;
cout << "nl = " << nl << endl;

double d = l;
cout << "d = " << d << endl;

double r = d / l;
cout << "r = " << r << endl;

double rr = l - d;
cout << "rr = " << rr << endl;

}

--------
output:

8
8
4
l = 9223372036854775807
nl = -9223372036854775808
d = 9.22337e+18
r = 1
rr = 0

-------------------
Nov 13 '05 #1
1 2663
Nishant Deshpande wrote:

On Solaris 64-bit:

sizeof(long) = 8
sizeof(double) = 8 !!!

So i figure i can't store a long with the same precision in a double.


You can store SOME longs in a double without loss os precision,
but definitely not all longs.

In a double, I believe that 13 bits are used for the exponent and
one for the sign. These take away from the mantissa, so the largest
64 bit long that can be represented as a double is 51 bits long
or

-(2^51) to (2^51) - 1

If you look at this a little more closely, it is actually less
than 1 in 2^13 of all valif 64 bit longs.

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo no****@mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+
"The Internet interprets censorship as damage, and routes around it."
-- John Gilmore
Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: dan | last post by:
Can someone tell me the difference between these three data types? Thanks Daniel
0
by: Zorba.GR | last post by:
IBM DB2 Connect Enterprise Edition v8.2, other IBM DB2 (32 bit, 64 bit) (MULTiOS, Windows, Linux, Solaris), IBM iSoft Commerce Suite Server Enterprise v3.2.01, IBM Tivoli Storage Resource Manager...
4
by: Michael Mair | last post by:
Hi there, actually, I have posted the same question in g.g.help. As there were no answers, I am still not sure whether this is a bug or only something open to the compiler that is seemingly...
22
by: bq | last post by:
Hello, Two questions related to floating point support: What C compilers for the wintel (MS Windows + x86) platform are C99 compliant as far as <math.h> and <tgmath.h> are concerned? What...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
6
by: James Thurley | last post by:
According to the docs, floats are 32 bit and doubles are 64 bit. So using floats should be faster than using doubles on a 32 bit processor, and my tests confirm this. However, most of the Math...
17
by: kiplring | last post by:
float sum = (float)Math.Sqrt( floatA*floatA + floatB*floatB); I'm using DirectX with c#. But the Math class in .net framework has a problem. It is "double" base! So I'm doing type casting...
10
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
67
by: lcw1964 | last post by:
This may be in the category of bush-league rudimentary, but I am quite perplexed on this and diligent Googling has not provided me with a clear straight answer--perhaps I don't know how to ask the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.