-1

I'm getting some values from different functions, some floats, other doubles, long...

I need to operate with them.

Is it correct to do it without previously converting to the same kind of number?

For example:

float distanceInMetersFloat = initialPosition.distanceTo(finalPosition);
long deltaTimeNanosLong = (finalPosition.getElapsedRealtimeNanos() - initialPosition.getElapsedRealtimeNanos()); // nanoseconds
long deltaTimeSecondsLong = deltaTimeNanosLong / 1000000000;

double speed = distanceInMetersFloat / deltaTimeNanosLong;

I have much more precision than I need, it doesn't matter to me to lose some decimal numbers.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Daniel Viaño
  • 485
  • 1
  • 9
  • 26
  • Its a very primitive question and probably one of the very first thing programmers are taught during training. – Adeel Jun 30 '16 at 06:29
  • I'm not a programmer, but a engineering student. I come from Matlab, where you don't have to define this. I'm learning, I don't know why you vote down. – Daniel Viaño Jun 30 '16 at 17:19

3 Answers3

3

I am going to attempt to guess what you want. The important parts are these:

  1. You must perform the math operation using as high a precision as you have. So in this case your original code is actually fine (though speed is a double which is right now useless unless you make distanceInMetersFloatalso a double).

  2. Once you have speed you can then convert it to whatever precision you want using whatever rounding mode you wish. For details on how to accomplish this in java see answer How to round a number to n decimal places in Java
Community
  • 1
  • 1
Creos
  • 2,445
  • 3
  • 27
  • 45
2

float and ints are 32 bits primitives, so if you want to make a Salad, prepare the bowl to hold them all? (double and long), additionally for that, longs and ints are whole numbers, so you must carefully and explicitly cast the numbers otherwise you will get weird results...

azurefrog
  • 10,785
  • 7
  • 42
  • 56
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

Be clear that you are mixing 32-bit values and 64-bit numbers. So you need to work towards ending up with the bigger 64-bit results t avoid losing data.

And be aware that you are mixing integer numbers with fractional numbers.

On both issues above, study carefully how Java primitives syntax works with numeric literals.

So yes you can mix the types. Java will generally do the right thing. For example, multiplying a long by an int will produce a long. But there are cases in complicated code where the compiler may get confused, and you will need to add some explicit casts such as ( long ). Any basic intro-to-Java book or the Oracle Tutorial will walk you though this.

Tips:

  • Append an L to long literals.
  • Append f to float literals. And d to double literals.
  • Use underscores to group your digits for easier reading. Ex: 1_000_000

Lastly, be aware that float and double are both floating-point numbers. Floating-point technology trades off accuracy for performance. This is good for rendering curves in a drawing or rendering a game, but bad for money or other values where you don't want extraneous extra digits in your decimal fraction.

BigDecimal

If accuracy matters, I would convert all your values to BigDecimal objects rather than primitives. BigDecimal eschews floating-point. Much slower, but accurate.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154