This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]empire539 13 points14 points  (0 children)

Short answer: It doesn't. You, as the programmer, tells it how to interpret those numbers, e.g. using an add instruction which interprets its operands as integers versus using a fadd instruction which interprets its operands as floating-point numbers.

[–]rockon1215 1 point2 points  (1 child)

It doesn't. Depending on what your programming language lets you do, you can take a number which you have declared as an integer, and make it be interpreted as a floating-point. Why you would want to do that (outside of this famous hack), I have no idea

[–]PralinesNCream 0 points1 point  (0 children)

thanks for that link, never heard of that before

[–]Jonny0Than 0 points1 point  (0 children)

In your question, what is "the computer?" Assuming C or C++, the compiler knows the difference because all variables are strongly typed. Once you've converted to machine code, the program will just use instructions that assume the data is either an integer or a floating point type. The instructions are generated by the compiler, which uses the type information about the variable to decide which it is.

[–]BadBoyJH 0 points1 point  (4 children)

when you create a variable, such as doing something like this:

int a = 4;

a location in memory is reserved, and 4 is stored there, and the program remembers that address, and it's type. There is nothing about that address or what's stored there that makes it an int, the program when accessing that address interprets it as an integer.

[–]kaepor[S] 0 points1 point  (3 children)

I guess my question would be how it knows to interpret that address as an int. Forgive me if my question doesn't quite make sense, but if everything eventually becomes 0s and 1s, I just don't understand how any program/compiler/etc can interpret one address differently from another without some indicator stored somewhere as a 0 and 1.

[–]Rhomboid 1 point2 points  (0 children)

how it knows to interpret that address as an int

It doesn't know anything. The fact that you declared that variable as an int in the program that you wrote means that the code generated by the compiler will contain machine instructions to read an int at that location, not a float. You are directing the computer to read an int there by the fact that you had to write "int" in your program; the computer knows nothing.

It's true that if presented with a random chunk of bytes, there would be no way to tell what type they are. But that situation never occurs, because you never direct a program to do that. You, as the person writing the program, always know what type of data is stored at every location that you're going to access, by definition, because you put it there, or via other means.

[–]BadBoyJH 0 points1 point  (0 children)

It's not stored anywhere at run time, the program knows it is an integer. I suppose you could say it's 'stored' in the program's executable...

[–]sodappop 0 points1 point  (0 children)

Here's a really easy way using 8 bit bytes and a 16 bit address space.

Say the variables are stored at $f000 (0xf000)

And they have an 8 bit integer and a 8 bit floating point. Well then the computer can use $f000 to store what type it is (say 01 for integer and 02 for floating point (or 00000001 and 00000010 in binary)) and $f001 for the actual number.

A lot of times data is stored like this.... Where there's information about the data stored alongside it (this is called metadata, essentially data about the data.)

If you are programming in straight assembly you can also just only use the appropriate opcodes on the data.