Solved

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

Posted on 2011-02-11
4
566 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

932 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now