Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Мимика Максим. Задание 02_10 #8963

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions 02-java-types/task10/src/com/example/task10/Task10.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,14 @@
public class Task10 {

public static boolean compare(float a, float b, int precision) {

// TODO корректно сравнивать два значения типа float с заданной пользователем точностью (параметр - количество знаков после запятой).
// Функция должна корректно обрабатывать ситуацию со сравнением значений бесконечности.
// Функция должна считать значения «не число» NaN (например 0.0/0.0) равными между собой.

return a == b;

if (Double.isNaN(a) && Double.isNaN(b)) return true;
if (Double.isInfinite(a) && Double.isInfinite(b))
return Math.signum(a * b) == 1;
precision = precision == 0 ? 1 : precision;
return Math.abs(a - b) <= Math.pow(10, -precision);
}

public static void main(String[] args) {
float a = 0.3f;
float b = 0.4f;
float sum = a + b;
float c = 0.7f;

boolean result = compare(sum, c, 2);
System.out.println(result);

System.out.println(compare((0.4f + 0.3f), 0.7f, 2));
}

}