Solved

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

Posted on 2011-02-11
4
569 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
[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
  • 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 44
is twain_32.dll cmpatible with windows 10 ? 10 272
GUI: DIalog Stacking and Popping in MS C++ 4 97
Outlook 13 100
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 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