DefNeo
Level 36

Level 30

Published in the Random EN group
Level 30
  1. What is NaN?

    NaN (English Not-a-Number) is one of the special states of a floating-point number. Used in many math libraries and math coprocessors. This state can occur in various cases, for example, when the previous mathematical operation ended with an undefined result, or if a number that does not satisfy the conditions got into the memory cell.

    In accordance with IEEE 754, this state is specified by setting the exponent to the reserved value 11…11, and the mantissa to anything other than 0 (reserved value for machine infinity). The sign and mantissa can carry some additional information: many libraries output "negative" NaN as -NaN.

    Operations that result in NaN as a response include:

    • all mathematical operations containing NaN as one of the operands;
    • division of zero by zero;
    • dividing infinity by infinity;
    • multiplication of zero by infinity;
    • addition of infinity to infinity of the opposite sign;
    • calculating the square root of a negative number[1];
    • logarithm of a negative number.

    Some programming languages ​​have “quiet” and “signal” NaNs: the first one, having got into any operation, returns NaN, the second one causes an emergency situation. Usually "quiet" or "signal" is determined by the most significant bit of the mantissa.

    NaN is not equal to any other value (not even itself[2]); accordingly, the simplest method of checking the result for NaN is to compare the resulting value with itself.

    The behavior of other comparison operators varies by language. Some languages ​​give false[3] (so a < b and b > a behave differently with NaN), others throw a crash even for "silent" NaN.

    Any non-trivial operation that takes a "quiet" NaN as an argument always returns NaN, regardless of the value of the other arguments. The only exceptions to this rule are the max and min functions, which return the value of the "second" argument (other than NaN). Trivial operations that are an identity are treated differently: for example, 1NaN is equal to 1.

  2. How to get infinity in Java?

    In Java, type doublehas special meanings for plus infinity and minus infinity. A positive number divided by 0.0 is "plus infinity", while a negative number is "minus infinity". These concepts correspond to special type constants Double:

    Code Description
    public static final double POSITIVE_INFINITY = 1.0 / 0.0; plus infinity
    public static final double NEGATIVE_INFINITY = -1.0 / 0.0; minus infinity
    1. We convert the string to a number, and it contains letters. The answer is NaN
    2. Infinity minus infinity. The answer is NaN
    3. Many other situations where a number is expected in the answer, but it turns out who knows what.

    Any operation involving NaN results in NaN.

    Actions with infinity:
    Expression Result
    n ÷ ±Infinity 0
    ±Infinity × ±Infinity ±Infinity
    ±(not zero) ÷ ±Infinity
    Infinity + Infinity Infinity
    ±0 ÷ ±0 NaN
    Infinity - Infinity NaN
    ±Infinity ÷ ±Infinity NaN
    ±Infinity × 0 NaN
  3. How to check that the result of the calculation is infinity?

    There is an answer on StackOverFlow .

    It all comes down to a conclusionSystem.out.println()

  4. What is a bitmask?

    A bit mask is when many different boolean values ​​(true/false) are stored as a single integer. In this case, each boolean value corresponds to a certain bit.

  5. Where are bit masks used?

    Basically, where you need to compactly store a lot of information about objects. When you store a lot of information about an object, there will always be a couple of dozen logical variables. It's convenient to store them all in one number. It is to store. Because using them at work is not so convenient.

  6. How to set a bit to one in a bitmask?

    Based on the lecture, you can answer with the following code:

    Here I used the method Integer.toBinaryString()in order to test myself, but suddenly)

    public class BitMask {
    
        public static void main(String[] args) {
            int a = 9;
    
           a |= (1<<2); // установить в 1 бит 2
    
    
            System.out.println(Integer.toBinaryString(a) + " "+ a);
        }
    }

    The conclusion is:

    1101 13

  7. How to set a bit to zero in a bitmask?

    public class BitMask {
    
    public static void main(String[] args) {
    int a = 15;
    
    a &= ~(1<<2); // установить в 0 бит 2
    System.out.println(Integer.toBinaryString(a) + " "+ a);
    
        }
    }

    Conclusion:

    1011 11

    I took the number 15, since it shows more clearly where 0 is set.

  8. How to get the value of a specific bit in a bitmask?

    public class BitMask {
    
    public static void main(String[] args) {
         int a = 15;
    
         a &= ~(1<<2); // установить в 0 бит 2
    
         int c = a & (1<<2); // узнаем про 2 бит
         int d = a & (1<<3); // узнаем про 3 бит
        System.out.println(Integer.toBinaryString(a) + " "+ a + " " + c +" "+ d);
    
        }
    }

    Conclusion:

    1011 11 0 8

    C 0 everything is clear, in that place it really is 0. And the variable d returns the value of the requested bit (in the 10th system).

  9. What is lazy expression evaluation?

    Article: Lazy programming and lazy evaluation

    These are lazy evaluations. In lazy evaluation, no parameter is evaluated unless it is needed. Programs actually start from the end and work from the end to the beginning. The program calculates what should be returned and continues backwards to determine what value it needs. In essence, every function is called with promises for every parameter. When a value is needed for the calculation, then the promise is executed. Because code is only executed when a value is needed, this is called call-by-need. In traditional programming languages, values ​​are passed instead of promises, this is called call-by-value.

    Call-by-need programming technology has a number of advantages. Flows are implemented automatically. Unnecessary values ​​are never evaluated. However, the behavior of lazy programs is often difficult to predict. In "call by value" programs, the order of evaluation is fairly predictable, so any time- or sequence-based computation is relatively easy to implement. In lazy languages, where special constructs such as monads are needed to describe explicitly ordered events, this is much more difficult. All this also makes communication with other languages ​​more difficult.

    There are programming languages ​​like Haskell and Clean that use lazy programming by default. Also, there are lazy versions for some languages ​​like Scheme, ML and others.

    Sometimes, by postponing calculations until the value is needed, you can optimize the speed of the program or restructure the program into a more understandable form. Despite their value, lazy programming techniques are not widely used or even well known. Consider adding them to your arsenal.

  10. What is the difference between using &&and &for type boolean?

    &&is a logical "and". (In this case, lazy evaluation takes place: some evaluation is omitted when the result is already clear)

    &is a bitwise "and" (If you apply this operator to Boolean variables, then lazy evaluation will not occur)

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION