Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 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

13 Experts available now in Live!

Get 1:1 Help Now