Solved

Is it possible to overload the double = (float) operator?

Posted on 2011-02-11
4
568 Views
Last Modified: 2012-05-11
Is it possible to overload the double = operator?

We're having some trouble with calculations in our software because of the issues converting floats to doubles.  Nearly all variables are floats but there are thousands of calls to DLLs that require doubles and the slight differences are causing problems.  

For Example:
float fVal = 7.f;
double val = fVal;

Open in new window

The double ends up coming out to something like 7.00000051351

One solution is to convert all our float variables to doubles, but you can imagine how messy that could get.

Maybe I'm barking up the wrong tree and the operator I need to overload is the float () operator to cast it into a double.

I'm not sure if it's even possible.  Just wondering if anyone has any brilliant solution for this.  I haven't found anything after alot of searching.

0
Comment
Question by:bigsteve87
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 34873095
The assignment operator (or any other operator for that matter) cannot be overloaded for basic types.

If you really want to do this, then you can wrap the float (or double) in a class, and provide an assignment operator for that class. You can then do anything you want in that assignment operator.


However, that wouldn't be the approach I would take. The problem is inherent to floating point types, so either :

(a) consistently use floating point types of the same width (no mixing of float and double)

(b) don't use floating points. Fixed point types can be a better choice. They provide arbitrary precision, and can avoid many of the rounding/precision related problems that floating point types have. The downside is that operations on them are generally slower than floating point types.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 34875625
I tried your OP code, and just got 7 (using cout). I've never tried the following before (until now), and not sure if this is OK, so consider it lightly. I added the line
#define float double
and reran the program, and still got 7. I didn't prove anything changed. And since I never did this before, I haven't thought of the ramifications. But if it is legit, then you could add this line to a header file that is included everywhere.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34875728
If you want to change all usage of float to double indiscriminately, it's probably better to do a global find/replace, rather than using a non-intuitive pre-processor macro that is likely to cause a lot of confusion, and will be a maintenance nightmare.
0
 

Author Comment

by:bigsteve87
ID: 34875765
I figured there wasn't an easy way around it.  A global replace is what needs to be done.  Thanks for the help.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

828 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