1 view
What is the result of subtracting 1 - 0.7 - 0.3, and why?
Asked at
AI answer
The result is usually close to 5.551115123125783e-17, not exactly zero. Binary floating-point cannot represent the decimal fractions 0.7 and 0.3 exactly, so subtraction introduces a rounding error. For monetary values, it’s better to store an integer number of the smallest units (for example, cents) or use a decimal arithmetic library.
Also asked at AutoSpot
- Tell us about yourself as a developer — experience, stack, projects.
- What are your salary expectations?
- Do you have experience writing and working with native SQL queries or similar low-level commands to a database?
- What AI tools do you use and for what tasks?
- What do you know about indexes? How can you determine which indexes are used in a query, and how can you tell which indexes are unnecessary or missing?
Similar questions
- How should you handle calculations like these (for example, with money) to avoid ambiguity?
- Why can't money be stored as float?
- Какой будет результат вывода? Почему? ```php class Foo { protected static $name = 'Foo'; public static function getName() { return static::$name; } } class Bar extends Foo { protected static $name = 'Bar'; } echo Bar::getName(); ``` Что будет выведено и почему? ```php $controlScore = 1; $a = 0.7; $b = 0.3; $result = $controlScore - $a - $b; if ($result > 0) { echo 'Access granted!'; } else { echo 'Access denied!'; } ```
- How can you make the second part of the previous problem produce 3, without using return or passing an object?
- What does empty() return for an empty string and zero?