representations

L

Lane Straatman

I'm trying to think of what one-third looks like when represented by two's
complement. What is its length? I'm not schur. It would be a DWORD in
assembly. In c, it would be (I think:)
float r = (float)(one third)
My syntax is somewhat wanting.

I think the question can be stated unambiguously if I ask what one-third
looks like with two's complement representation and a length of 32 bits.
 
A

Army1987

Lane Straatman said:
I'm trying to think of what one-third looks like when represented by two's
complement. What is its length? I'm not schur. It would be a DWORD in
assembly. In c, it would be (I think:)
float r = (float)(one third)
My syntax is somewhat wanting.

I think the question can be stated unambiguously if I ask what one-third
looks like with two's complement representation and a length of 32 bits.

Two's complement is a representation for signed integers.
What did you *really* mean?
 
L

Lane Straatman

Army1987 said:
Two's complement is a representation for signed integers.
What did you *really* mean?
I knew I was in trouble there. What possiblities does one have for
representing one-third as a float in C, in particular with 32 bits?
 
G

Guest

Lane said:
I knew I was in trouble there. What possiblities does one have for
representing one-third as a float in C, in particular with 32 bits?

Every possible bit pattern is a potential valid representation of the
best approximation to one-third, or even the exact representation of
it. There are practically no requirements on how a float is stored in
memory.

As Army1987 asked, what did you *really* mean?
 
M

Malcolm McLean

Lane Straatman said:
I knew I was in trouble there. What possiblities does one have for
representing one-third as a float in C, in particular with 32 bits?
Do you know what sign, mantissa and exponent mean, in relation to floating
point numbers?
 
F

Fred Kleinschmidt

Lane Straatman said:
I knew I was in trouble there. What possiblities does one have for
representing one-third as a float in C, in particular with 32 bits?

00111110101010101010101010101011
 
L

Lane Straatman

Harald van D?k said:
Every possible bit pattern is a potential valid representation of the
best approximation to one-third, or even the exact representation of
it. There are practically no requirements on how a float is stored in
memory.
That's what I thought. How is it usually represented?
--
 
L

Lane Straatman

Malcolm McLean said:
Lane Straatman said:
Army1987 said:
"Lane Straatman" <[email protected]> ha scritto nel messaggio
I'm trying to think of what one-third looks like when represented by
two's complement. What is its length? I'm not schur. It would be a
DWORD in assembly. In c, it would be (I think:)
float r = (float)(one third)
My syntax is somewhat wanting.

I think the question can be stated unambiguously if I ask what
one-third looks like with [some reasonable representation] and a length
of 32 bits.

Two's complement is a representation for signed integers.
What did you *really* mean?
I knew I was in trouble there. What possiblities does one have for
representing one-third as a float in C, in particular with 32 bits?
Do you know what sign, mantissa and exponent mean, in relation to floating
point numbers?
There was a recent thread with Joe Wright and others that is probably as
much as I know about it. The sign will take a bit. The mantissa is the a
in the a**x. The exponent is the exponent, x.
--
 
A

Al Balmer

That's what I thought. How is it usually represented?

At the risk of spoiling all the fun, I suggest you Google for "IEEE
floating point", which should get you lots of information.
 
M

Martin Ambuhl

Lane said:
That's what I thought. How is it usually represented?

#include <stdio.h>
#include <string.h>
#include <float.h>

int main(void)
{
float fltvalue = 1. / 3.;
double dblvalue = 1. / 3.;
long double lngdblvalue = 1.L / 3.;

unsigned char fltchars[sizeof fltvalue], dblchars[sizeof dblvalue],
lngdblchars[sizeof lngdblvalue];
size_t i;

printf("Every implementation is free to choose its own\n"
"method for storing floating point values.\n"
"Here is how one implementation stores 1/3.\n");

memcpy(fltchars, &fltvalue, sizeof fltvalue);
printf("As float 1/3 = %.*g\nHex: ", FLT_DIG, fltvalue);
for (i = 0; i < sizeof fltvalue; i++)
printf("%u ", fltchars);
printf("\n\n");


memcpy(dblchars, &dblvalue, sizeof dblvalue);
printf("As double 1/3 = %.*g\nHex: ", DBL_DIG, dblvalue);
for (i = 0; i < sizeof dblvalue; i++)
printf("%u ", dblchars);
printf("\n\n");

memcpy(lngdblchars, &lngdblvalue, sizeof lngdblvalue);
printf("As long double 1/3 = %.*Lg\nHex: ", LDBL_DIG, lngdblvalue);
for (i = 0; i < sizeof lngdblvalue; i++)
printf("%u ", lngdblchars);
printf("\n\n");

return 0;
}


Every implementation is free to choose its own
method for storing floating point values.
Here is how one implementation stores 1/3.
As float 1/3 = 0.333333
Hex: 171 170 170 62

As double 1/3 = 0.333333333333333
Hex: 85 85 85 85 85 85 213 63

As long double 1/3 = 0.333333333333333333
Hex: 171 170 170 170 170 170 170 170 253 63 0 0
 
M

Malcolm McLean

There was a recent thread with Joe Wright and others that is probably as
much as I know about it. The sign will take a bit. The mantissa is the a
in the a**x. The exponent is the exponent, x.
Nearly right.
IEEE standard floating point number use something very similar to scientific
notation.
2000 is 2 * 10^3 in this notation.
By convention there is only one decimal digit before the point, so
2300 is 2.3 * 10^3
Numbers below one have negative exponents
0.023 = 2.3 * 10^-2 (mnemonic, count the zeroes, including the leading one)

2.3 itself is 2.3 * 10^0.

Floating point numbers are basically just the same, but in binary. Since the
leading most significant binary digit must always be one, most
representations including IEEE save a bit by omitting it. There is one
exception to this rule, zero, so zero is represented specially by all bits
zero. Since zero is 10^-inf, it makes sense to store exponents in a sign
magnitude encoding rather than twos complement, so that 0 is the lowest, 1
= -127 or whatever, and so on. The sign bit is self explanatory.
Finally there are a few special representations for infinity, not a number,
and what are called denormalised numbers, but you can ignore those for now.

By looking at the bit patterns in your compiler, you should be able to work
out how many bits are allocated to each section, and what the exponent
encoding is. Start with one and minus one to get the sign bit.
 
K

Keith Thompson

Lane Straatman said:
I'm trying to think of what one-third looks like when represented by two's
complement. What is its length? I'm not schur. It would be a DWORD in
assembly. In c, it would be (I think:)
float r = (float)(one third)

float r = 1.0/3.0;
My syntax is somewhat wanting.

I think the question can be stated unambiguously if I ask what one-third
looks like with two's complement representation and a length of 32 bits.

It looks like this:

00000000000000000000000000000000

Two's-complement is a representation for signed integers, which cannot
represent fractional values.

If you're asking what 1.0/3.0 looks like in a floating-point
representation, it depends on which representation is used. The IEEE
standard is most common; a web search should turn up documentation.
 
K

Keith Thompson

Keith Thompson said:
float r = 1.0/3.0;

[snip]

I thought Lane Straatman's name looked familiar; I should have checked
before posting a followup.

Lane Straatman, in my opinion, is a troll; see
<http://groups.google.com/group/comp.lang.c/msg/a4cc7d2a141439a7>,
which references
<http://groups.google.com/group/comp.std.c/msg/d91bae8783a38bde>.

Until and unless he publicly apologizes for his previous boorish
behavior, he'll get no further help from me. Others, as always, will
do as they wish.
 
O

Old Wolf

printf("As float 1/3 = %.*g\nHex: ", FLT_DIG, fltvalue);
for (i = 0; i < sizeof fltvalue; i++)
printf("%u ", fltchars);

Here is how one implementation stores 1/3.
As float 1/3 = 0.333333
Hex: 171 170 170 62


That's an unconventional use of "Hex" :)
 
O

Old Wolf

The sign will take a bit. The mantissa is the a in the a**x.
The exponent is the exponent, x.

In the implementation I have looked at, it is
a * 2**x

where 'a' is represented as a binary fraction between 0 and 1,
for example 0.75 would be 110000...0

Note that you would never need 'a' being less than 0.5, because
you could then scale it by doubling 'a' and subtracting one from 'x'.
So you can actually drop the first bit of 'a', assuming it always
to be 1, and use that storage for the sign bit.
 
L

Lane Straatman

Keith Thompson said:
Keith Thompson said:
float r = 1.0/3.0;

[snip]

I thought Lane Straatman's name looked familiar; I should have checked
before posting a followup.

Lane Straatman, in my opinion, is a troll; see
<http://groups.google.com/group/comp.lang.c/msg/a4cc7d2a141439a7>,
which references
<http://groups.google.com/group/comp.std.c/msg/d91bae8783a38bde>.

Until and unless he publicly apologizes for his previous boorish
behavior, he'll get no further help from me. Others, as always, will
do as they wish.
Your "help" has consisted of shitting on my threads; your abscence is
welcome. I liked you a lot more as the Other Keith.
--
 
M

Martin Ambuhl

Old said:
printf("As float 1/3 = %.*g\nHex: ", FLT_DIG, fltvalue);
for (i = 0; i < sizeof fltvalue; i++)
printf("%u ", fltchars);

Here is how one implementation stores 1/3.
As float 1/3 = 0.333333
Hex: 171 170 170 62


That's an unconventional use of "Hex" :)


Yes, I erred. Thanks for noticing.
To the OP: my "%u " should be "%x ".
 
L

Lane Straatman

Fred Kleinschmidt said:
00111110101010101010101010101011
I'm actually going to try to code this tonight, but there's no guarantees
because I've got a poker game to go to. I think that Fred's answer is going
to be what my C IDE on windows has. I would be surprised to find popular
implementations that don't waffle between zero and one on this for the
bigger part of it.

It's been a while since I put a semicolon on the end of an executable
statement.
 
C

CBFalconer

Lane said:
.... snip ...

Your "help" has consisted of shitting on my threads; your abscence
is welcome. I liked you a lot more as the Other Keith.

Looks like Keith was right - PLONK.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top