1

Is it possible to use some construct to replace all floats with doubles (or the opposite) without refactoring?

For example you may be implementing some mathematical system that works perfectly interchangeably with floats or doubles. In C you may use: typedef float real and then use real in your code. Changing to double involves only replacing one line of code.

Is something like this possible in Java? Or is there some generic numeric type?

tckmn
  • 57,719
  • 27
  • 114
  • 156
zduny
  • 2,481
  • 1
  • 27
  • 49

4 Answers4

2

This is not possible in Java in the straightforward case which you describe. However, depending on how your code works, you could write your math classes to interfaces, and have all methods that return values be implemented with both a double and a float return type. Then, you could write two implementation classes, and switch between them depending on which one you wanted to use.

This seems like overkill. Why do you want to do this?

durron597
  • 31,968
  • 17
  • 99
  • 158
0

It's actually recommended to use BigDecimal instead of float/double. Don't think java has something similar to typedef float real

Dev Blanked
  • 8,555
  • 3
  • 26
  • 32
  • The performance problems with BigDecimal are the String operations to convert between the two, not BigDecimal math. – durron597 May 03 '13 at 13:17
  • see there are accuracy concerns specially when you are dealing with monetary values....http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – Dev Blanked May 04 '13 at 06:57
  • basically if accuracy is your top most priority don't use float/double use BigDecimal – Dev Blanked May 04 '13 at 06:59
0

You can use object-oriented aproach.

Create your own class that implements the methods your mathematical system needs. Use this class instead of float. Inside it can use whatever you want float, double or BigDecimal. You can change later how your class works without changing the rest of your system.

Take a look at Double, it will give the general idea how to build it.

Implement methods for addition, multiplication etc.

E.g.:

public class MyDecimal
{
    private float value;

    public MyDecimal(int value)
    {
        this.value = value;
    }

    public MyDecimal(float value)
    {
        this.value = value;
    }

    public MyDecimal multiply(MyDecimal by)
    {
        return new MyDecimal(value * by.value);
    }

    ...
}

So, if you want to use double instead of float you only need to change this class.

denis
  • 417
  • 3
  • 8
  • No, it doesn't allow you to switch between using `float` or `double` without refactoring. It is built like the sample above to achieve better precision at the cost of performance. So there are situations where `BigDecimal` is better than `float`/`double` but there could be situations where it is worse (for example a mathematical system with many calculations where precision isn't needed). – denis May 04 '13 at 20:19
  • One could always create a BigDecimal instance from a string. either float or double can be converted to a string – Dev Blanked May 05 '13 at 05:49
  • If the question is what is a better numerical type for a particular situation, `BigDecimal` could be the answer (knowing precision and performance requirements). If the question is how to build a system which can be easily changed to work with `float` instead of `double` and vice-versa, `BigDecimal` will not help. – denis May 05 '13 at 20:53
0

No, there is no way to achieve this in Java with primitive types. There is simply no typedef equivalent and there are also no template classes. From a functional view, you could work this with the object oriented way, the methods would take a wrapper class/interface type (something like java.lang.Number) and also return results as a wrapped type.

However, I would just scrap the entire idea and only implement the double version. Callers that want to work with float can just use the double version of any method - parameters will be automatically widened to double. The results then need to be cast back to float by the caller. The conversions to and from double will cost a little speed. Or if double was just nice to have and you can make do with float, create only a float version.

In terms of raw computation speed, there is little to no difference between float and double (on a desktop CPU). The speed advantage with float usually mostly comes from the halved memory bandwidth requirements.

If its just one or a few utility classes, you could also have two sets of them (e.g. FloatMathUtil and DoubleMathUtil). It would then be up to the user to decide which one to code against (they would be entirely unrelated classes in terms of API).

Durandal
  • 19,919
  • 4
  • 36
  • 70