Solved

Convert UINT64 to double (MSVC6)

Posted on 2004-09-15
6
2,298 Views
Last Modified: 2008-01-09
Dear Experts,

MSVC6 can not internally convert unsigned __int64 to double. However, VC.NET 2003 (among others) can do the conversion. In addition GCC for Intel Architectures can internally convert unsigned long long, also 64-bit unsigned integer, to double.

Does anyone have a good work-around for MSVC6? I'm using the code snippet shown below, but I find it unsatisfactory. First of all, the code does not use the full range, since for MSVC6 there is a conversion to signed 64-bit integer, and also the snippet is ugly and disturbs the more 'normal' use of static_cast<double>(UINT64 u64). Is it possible to make a template specialization for static_cast<double>(UINT64 u64) specifically for MSVC6? Alternatively, does anyone know the compiler intrinsic mathematics for the proper conversion of UINT64 to double so that I can at least correct my code snippet?

Thanks for your help.

Sincerely, Chris.

inline double UINT64_to_double(const UINT64& u)
{
  #if defined (_MSC_VER) && (_MSC_VER <= 1200)

    return static_cast<double>(static_cast<INT64>(u));

  #else

    return static_cast<double>(u);

  #endif
}
0
Comment
Question by:dude_1967
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 200 total points
ID: 12063429
This should do it on an Intel platform (i.e. assuming little-endian):

inline double UINT64_to_double(const UINT64& u)
{
#if defined (_MSC_VER) && (_MSC_VER <= 1200)
    static const double multiplier = static_cast<double>(std::numeric_limits<UINT32>::max())+1.0;
    const UINT32* p_low  = reinterpret_cast<const UINT32*>(&u);
    const UINT32* p_high = p_low+1;
    return *p_high*multiplier + *p_low;
#else
    return static_cast<double>(u);
#endif
}
0
 
LVL 22

Accepted Solution

by:
grg99 earned 200 total points
ID: 12063464
How many significant bits in a UINT64?    64 I'd guess.

How many significant bits in a double?   Less than 64.

What do you want to do if the UINT64 doesnt fit?



0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 200 total points
ID: 12063710
> What do you want to do if the UINT64 doesnt fit?

It is a floating point representation. See the following:
--------8<--------
#include <iostream>
#include <limits>
#include <iomanip>

typedef __int64 UINT64;

template<class T> void info(const char *name)
{
        std::cout
                << name
                << std::scientific
                << std::setprecision(3) /* Good enough for the max */
                << " data bits = " << std::numeric_limits<T>::digits
                << ", max = " << static_cast<double>(std::numeric_limits<T>::max())
                << '\n';
}

#define INFO(T) info<T>(#T)

int main()
{
      INFO(UINT64);
      INFO(double);
}
--------8<--------

UINT64 data bits = 63 max = 9.223e+018
double data bits = 53 max = 1.798e+308

double has a greater range because it uses an exponent.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Assisted Solution

by:grg99
grg99 earned 200 total points
ID: 12063754
Right, it has a wider RANGE, but TEN fewer bits of precision.  

So a random 64-bit integer has only one chance in 1024 of being accurately represented as a double.



0
 
LVL 1

Author Comment

by:dude_1967
ID: 12065751
Thank you for all of these clear responses. Indeed, I had never really taken the time to clearly think about this conversion. The 53 bits of the ieee754 double can not identically represent UINT64.

In fact, this recognition has caused me to have to re-think the developmental issue to which this question pertains. The development issue involves converting result = (u64 * 1000000) / scale. Previously I had done a 'cheap' conversion using the cast to double of 1000000.0. But upon reflection, I noted that I actually want to do an exact conversion. This is no problem, but I will have to dig into the tools for extended integers.

Thanks everyone for clearing up this issue.

All of the responses were helpful and I must split the points equally. I can't figure out how to increase the points.

This issue is, in my opinion, now closed.

Sincerely, Chris.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12066912
It might be worth your while looking at boost's big_int - see http://groups.yahoo.com/group/boost/files/big_integer/
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TCP/IP Socket - connection close results in data lost 14 153
c++, dynamic object by json 1 60
show out valin of json for debugging in visaul c++ 1 48
Finding Divisors 5 40
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question