what is meant by 'Most C system provide for logically infinite floating values'?

℡╲_俬逩灬. 提交于 2020-07-09 02:58:05

问题


Initially I declared variables x and y as type int:

#include<stdio.h>   

     int main(){
        int x, y = 0 ;
        x = 1 / y;
        printf("%d", x);

        return 0;    
    }

Program crashed (for obvious reasons).

Now I declared variables x and y as double:

#include<stdio.h>   

 int main(){
    double x, y = 0 ;
    x = 1 / y;
    printf("%d", x);

    return 0;    
}

But Output: 0. (why?)

Then I changed %d to %f in printf:

#include<stdio.h>   

 int main(){
    double x, y = 0 ;
    x = 1 / y;
    printf("%f", x);

    return 0;    
}

Output: 1.#INF00

I don't understand what is happening here.

Please explain me above cases.


回答1:


There are no numbers in a computer. We build computers out of physical parts, and we use physical properties to store and manipulate data.

In various places, a computer has electric charges, electric voltages, magnetic fields, or other physical things that we use to represent data. In doing this, we pick some physical state and call it “0” and some other state and call it “1”. These are merely convenient names. Mathematical numbers like 0 and 1 are abstract entities—they are concepts with no physical existence. The numbers 0 and 1 do not exist in computers.

We bundle these physical states, often in groups of eight, 32, or 64, and then label them in various ways. For example, with the eight bits in the states labeled 00100010, we might call that “34”. It is still not a number. We are merely using a binary notation for the number 34, and that binary notation further designates the state of the pieces of the machine.

The pieces of the machine in the state 00100010 are not inherently the actual number 34 any more than they are a banana or the concept of red.

We design parts of the machine so they can manipulate these states. Somewhere in the machine is an adder that takes as input physical states representing one number and physical states representing another number and creates as output physical states representing the number that is the sum of the two input numbers.

Computers contain many parts like these that create the effect of adding numbers, multiplying numbers, subtracting numbers, and so on. These are just effects created in a machine.

With floating-point numbers, we designate certain of the bit patterns to represent infinity, and we design the floating-point arithmetic unit to behave correspondingly. When we give the floating-point arithmetic adder one input that represents infinity and another input that represents a finite number, it produces an output that represents infinity, because we designed the floating-point arithmetic unit to do that. Similarly, when we give the floating-point divider one input that represents the number one and another input that represents the number zero, it produces as output the bit pattern that represents infinity, again because we designed it to do that.

When you print a floating-point object that as the bit pattern representing infinity using printf("%f", x);, the C implementation prints a string representing infinity. Microsoft’s chosen string for that is “1.#INF00”, which is rather ugly. Some other implementations use “inf”, which is only slightly better.

When you attempt to print a floating-point object using printf("%d", x);, the behavior is not defined by the C standard, because the %d conversion expects to receive an int object, but the x you are passing is a double object. Passing the wrong type of argument can screw up the argument-passing mechanism in a variety of ways, so you will not always get answers that make sense without knowing how the internals of the software work.




回答2:


Most systems you're likely to come in contact with use IEEE 754 representation for floating point numbers. That representation has ways to store the values +infinity and -infinity.

While strictly speaking, dividing by 0 is undefined behavior, implementations using IEEE 754 extend the language to allow it for floating point types. In this case, dividing by 0 can be considered infinity, so your implementation allows it, and 1.#INF00 is how MSVC prints the value for infinity.

Also, using the wrong format specifier for printing as in your second example where you use %d to print a double is undefined behavior. Format specifiers must match the datatype of what is passed in.



来源:https://stackoverflow.com/questions/60816945/what-is-meant-by-most-c-system-provide-for-logically-infinite-floating-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!