# Вопрос по java, c – Поплавок против Дабл

9

Есть ли когда-нибудь случай, когда сравнение (`equals()`) между двумя значениями с плавающей точкой будет возвращаться`false` если сравнить их как`DOUBLE` но вернись`true` если сравнивать их как FLOAT?

Я пишу некоторую процедуру, как часть моего группового проекта, для сравнения двух числовых значений любых заданных типов. В общей сложности 4 типа, с которыми мне приходится иметь дело:`double`, `float`, `int` а также`long`, Поэтому я хотел бы сгруппировать`double` а также`float` в одну функцию, то есть я просто`float` в`double` и сделай сравнение.

Приведет ли это к каким-либо неверным результатам?

Благодарю.

Error: User Rate Limit Exceeded user113454
Error: User Rate Limit Exceeded Tommy

Ваш Ответ

7   ответов
0

double and do a 64bit comparison rather than a 32bit comparison?

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

1

`abs((double) floatVal1 - (double) floatVal2) < .000001`

Edit in response to the question change

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
14

``````9.876543210
9.876543211
``````

`float``9.87654`

Error: User Rate Limit Exceeded`float` в`double`Error: User Rate Limit Exceeded user113454
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded`float`.
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededmy answer belowError: User Rate Limit Exceeded`double`Error: User Rate Limit Exceeded
2

``````result = a * b / c;
result = (a * b) / c;
result = a * (b / c);
``````

Error: User Rate Limit Exceeded
1

2

`==`

Error: User Rate Limit Exceeded
Нет, я упрощал мою проблему. Это больше, чем просто`overriding`Error: User Rate Limit Exceeded`==`Error: User Rate Limit Exceeded`float`Error: User Rate Limit Exceeded`double`Error: User Rate Limit Exceeded user113454
Error: User Rate Limit Exceededdon't указать продвижение от поплавка до удвоения для ==.
Error: User Rate Limit Exceeded user113454
`==` не может использоваться для безопасного сравнения двойных чисел в C - фактически, GCC выдаст предупреждение, если вы попытаетесь.
9

Comparing floats (or doubles) for equality is difficult - .

You can't use `==`, because of problems with the limited precision of floating point formats

float(0.1) and double(0.1) are different values (0.100000001490116119384765625 and 0.1000000000000000055511151231257827021181583404541015625) respectively. In your case, this means that comparing two floats (by converting to double) will probably be ok, but be careful if you want to compare a float with a double.

It's common to use an epsilon or small value to make a relative comparison with (floats a and b are considered equal if `a - b < epsilon`). In C, float.h defines `FLT_EPSILON` for exactly this purpose. However, this type of comparison doesn't work where `a` and `b` are both very small, or both very large.

You can address this by using a scaled-relative-to-the-sizes-of-a-and-b epsilon, but this breaks down in some cases (like comparisons to zero).

You can compare the integer representations of the floating point numbers to find out how many representable floats there are between them. This is what Java's `Float.equals()` does. This is called the ULP difference, for "Units in Last Place" difference. It's generally good, but also breaks down when comparing against zero.

Know what you’re doing

There is no silver bullet. You have to choose wisely.

If you are comparing against zero, then relative epsilons and ULPs based comparisons are usually meaningless. You’ll need to use an absolute epsilon, whose value might be some small multiple of FLT_EPSILON and the inputs to your calculation. Maybe. If you are comparing against a non-zero number then relative epsilons or ULPs based comparisons are probably what you want. You’ll probably want some small multiple of FLT_EPSILON for your relative epsilon, or some small number of ULPs. An absolute epsilon could be used if you knew exactly what number you were comparing against. If you are comparing two arbitrary numbers that could be zero or non-zero then you need the kitchen sink. Good luck and God speed.

If you are downgrading `double`s to `float`s, then you might lose precision, and incorrectly report two different `double`s as equal (as paxdiablo points out.) If you are upgrading identical `float`s to `double`, then the added precision won't be a problem unless you are comparing a `float` with a `double` (Say you'd got 1.234 in float, and you only had 4 decimal digits of accuracy, then the double 1.2345 MIGHT represent the same value as the float. In this case you'd probably be better to do the comparison at the precision of the `float`, or more generally, at the error level of the most inaccurate representation in the comparison). If you know the number you'll be comparing with, you can follow the advice quoted above. If you're comparing arbitrary numbers (which could be zero or non-zero), there's no way to compare them correctly in all cases - pick one comparison and know its limitations.

The epsilon comparison mentioned by most is probably fine (but include a discussion of the limitations in the write up). If you're ever planning to compare doubles to floats, try to do it in float, but if not, try to do all comparisons in double. Even better, just use `double`s everywhere.

If you want to totally ace the assignment, include a write-up of the issues when comparing floats and the rationale for why you chose any particular comparison method.

Error: User Rate Limit Exceededrandomascii.wordpress.com/2014/01/27/…Error: User Rate Limit Exceeded