Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-11
4
Medium Priority
?
573 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 2000 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

885 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