From: Grant Edwards on
On 2010-03-20, Habib Bouaziz-Viallet <h.bouazizviallet(a)free.fr> wrote:

>>> I said that extracting the integer part of the float is ok when
>>> casting a float to an int.
>>
>> That's what the OP was doing: casting the float to an 8-bit unsigned
>> integer.
>>
>>> When you make a cast from a float to an unsigned int it would result
>>> a zero if the float is negative (not quite sure either ... just
>>> conjecture)
>>
>> No, that's not how it works.
>
> No ?
>
> Please compile this (gcc -o test -c test -lm) and tell me what you
> see.

You said that negative float values convert to an unsigned value of 0.
Here's what your sample programs shows:

-5.627771
251

-9.410577
247

-9.602361
247

-6.129940
250

Negative float values do not convert to unsigned values of 0. IOW, it
doesn't work the way you said it did, and the example program you
posted proves it.

> I knew this was not a related compiler issue or what else (on this i
> agree with David), finally i give up because this thread is a serie
> of non-sense.

I agree that the OP's problem had nothing to do with float/int
conversion. The OP's problem is that he ran out of code space.


--
Grant

From: Hans-Bernhard Bröker on
Albert van der Horst wrote:
> In article <4ba32898$0$22919$e4fe514c(a)news.xs4all.nl>,
> Meindert Sprang <ms(a)NOJUNKcustomORSPAMware.nl> wrote:

>> Apart from that, as others have said, u = f should have worked too
>> when the integer part of f is in the range of 0-255.

> It works on any integer.

No, it doesn't. That's been clarified days ago in this thread.

> Unsigned char's are supposed to work modulo
> their range.

Yes, but that only applies once you _have_ an unsigned char. The
problem under consideration doesn't have one --- it's trying to build one.

> Now I think
> u = (unsigned char) f;
> is brain damaged.

You think incorrectly, there.

> This tells the compiler to convert the float converted to an integer,
> then take it modulo 256.

No, it doesn't. It instructs the compiler to generate code that does
exactly what the code says: convert the float to an unsigned char.
That's another thing that has been clarified days ago.

From: linnix on
On Mar 23, 12:35 pm, Hans-Bernhard Bröker <HBBroe...(a)t-online.de>
wrote:
> Albert van der Horst wrote:
>
> > In article <4ba32898$0$22919$e4fe5...(a)news.xs4all.nl>,
> > Meindert Sprang <m...(a)NOJUNKcustomORSPAMware.nl> wrote:
> >> Apart from that, as others have said, u = f should have worked too
> >> when the integer part of f is in the range of 0-255.

Yes, and we will scale f to fit within 255 first. We are only
interested in relative magnitude of an array.

> > It works on any integer.
>
> No, it doesn't.  That's been clarified days ago in this thread.
>
> > Unsigned char's are supposed to work modulo
> > their range.
>
> Yes, but that only applies once you _have_ an unsigned char.  The
> problem under consideration doesn't have one --- it's trying to build one..
>
> > Now I think
> > u = (unsigned char) f;
> > is brain damaged.
>
> You think incorrectly, there.

The compiler is smart enough to understand "u = f or i = f" as well.
The casting is for human reading the code. It may be unnecessary, but
not brain damaged.

>
> > This tells the compiler to convert the float converted to an integer,
> > then take it modulo 256.
>
> No, it doesn't.  It instructs the compiler to generate code that does
> exactly what the code says: convert the float to an unsigned char.
> That's another thing that has been clarified days ago.

It is mainly for storage. Unsigned char (8 bits) takes less space than
float (16 bits) or double (32 bits). We don't really care how the
compiler does it, but storage is important for a 1K SRAM chip.

From: Jamie on
linnix wrote:

> On Mar 23, 12:35 pm, Hans-Bernhard Br�ker <HBBroe...(a)t-online.de>
> wrote:
>
>>Albert van der Horst wrote:
>>
>>
>>>In article <4ba32898$0$22919$e4fe5...(a)news.xs4all.nl>,
>>>Meindert Sprang <m...(a)NOJUNKcustomORSPAMware.nl> wrote:
>>>
>>>>Apart from that, as others have said, u = f should have worked too
>>>>when the integer part of f is in the range of 0-255.
>
>
> Yes, and we will scale f to fit within 255 first. We are only
> interested in relative magnitude of an array.
>
>
>>>It works on any integer.
>>
>>No, it doesn't. That's been clarified days ago in this thread.
>>
>>
>>>Unsigned char's are supposed to work modulo
>>>their range.
>>
>>Yes, but that only applies once you _have_ an unsigned char. The
>>problem under consideration doesn't have one --- it's trying to build one.
>>
>>
>>>Now I think
>>>u = (unsigned char) f;
>>>is brain damaged.
>>
>>You think incorrectly, there.
>
>
> The compiler is smart enough to understand "u = f or i = f" as well.
> The casting is for human reading the code. It may be unnecessary, but
> not brain damaged.
>
>
>>>This tells the compiler to convert the float converted to an integer,
>>>then take it modulo 256.
>>
>>No, it doesn't. It instructs the compiler to generate code that does
>>exactly what the code says: convert the float to an unsigned char.
>>That's another thing that has been clarified days ago.
>
>
> It is mainly for storage. Unsigned char (8 bits) takes less space than
> float (16 bits) or double (32 bits). We don't really care how the
> compiler does it, but storage is important for a 1K SRAM chip.
>
Last time I knew, that just cast the object into what you want, but it's
not going to magically convert a FLOAT to a unsigned char..
Its only going to instruct the compiler that the contents of memory the
"F" occupies is a unsigned char and simply moves the data from that
from ever it is in memory to "u"
simply speaking..
u = f; isn't the same as u = (unsigned char) f; where I come from..

And the last time I knew, AVR's do not have native support for
floating point math. UNless I've been out of the loop here?
Instead, the compiler should be loading a default RTL that contains
low level code to give you that results.. This of course is compiler
linked and thus, makes it look natural to you, the coder.

And if the C compiler is standardized like most other compilers..

u = (unsigned char) f; simply tells the compiler to treat the object
of "F" as if it was a char instead. No conversion is taking
place here.
As far as how the floats are implemented for the binary format of
this compiler will dictate what value of data will be returned..

I can only assume it may support the Mantissa and other elements of a
floating point number, in which case, the above will not work as you may
think..
But who am I, I don't know anything..

stick with the "u = f;" and the compiler should be happy with that and
call one of it's floattointeger conversion TRL functions.. But If you
want to know how the float if constructed to represent a floating point
number, I guess you could hack it with the cast. I think only the
authors of the compiler would understand it's meanings..



From: Grant Edwards on
On 2010-03-24, Jamie <jamie_ka1lpa_not_valid_after_ka1lpa_(a)charter.net> wrote:

> Last time I knew, that just cast the object into what you want, but it's
> not going to magically convert a FLOAT to a unsigned char..

That's exactly what a cast does. (Though there's nothing magical
about the conversion -- it's a well-defined operation.)

> Its only going to instruct the compiler that the contents of memory the
> "F" occupies is a unsigned char and simply moves the data from that
> from ever it is in memory to "u"

No, it isn't. It instructs the compiler to do the conversion.

> simply speaking..
> u = f; isn't the same as u = (unsigned char) f; where I come from..

I'm not sure where you come from. Here on Earth, when using the C
language, it's exactly the same.

Here's an example program:


extern volatile unsigned char u1,u2;
extern volatile float f;

void foo1(void)
{
u1 = f;
}

void foo2(void)
{
u2 = (unsigned char)f;
}

And here's the generated code using gcc for IA32. Note that the code
generated for the two cases is identical:

.file "testit.c"
.text
.p2align 4,,15
.globl foo1
.type foo1, @function
foo1:
subl $8, %esp
fnstcw 6(%esp)
flds f
movzwl 6(%esp), %eax
movb $12, %ah
movw %ax, 4(%esp)
fldcw 4(%esp)
fistps 2(%esp)
fldcw 6(%esp)
movzwl 2(%esp), %eax
movb %al, u1
addl $8, %esp
ret
.size foo1, .-foo1
.p2align 4,,15
.globl foo2
.type foo2, @function
foo2:
subl $8, %esp
fnstcw 6(%esp)
flds f
movzwl 6(%esp), %eax
movb $12, %ah
movw %ax, 4(%esp)
fldcw 4(%esp)
fistps 2(%esp)
fldcw 6(%esp)
movzwl 2(%esp), %eax
movb %al, u2
addl $8, %esp
ret
.size foo2, .-foo2
.ident "GCC: (Gentoo 4.3.4 p1.0, pie-10.1.5) 4.3.4"
.section .note.GNU-stack,"",@progbits



> And the last time I knew, AVR's do not have native support for
> floating point math. UNless I've been out of the loop here?

Doesn't matter.

> Instead, the compiler should be loading a default RTL that contains
> low level code to give you that results.. This of course is compiler
> linked and thus, makes it look natural to you, the coder.

No idea what your point is.

> And if the C compiler is standardized like most other compilers..
>
> u = (unsigned char) f; simply tells the compiler to treat the object
> of "F" as if it was a char instead. No conversion is taking
> place here.

Yes, a conversion _is_ taking place here. That's what a cast _is_:
it's an explicit conversion operation.

> As far as how the floats are implemented for the binary format of
> this compiler will dictate what value of data will be returned..

No, it won't. It will take the integer portion of the float value and
store that value (modulo 256) into the unsigned char.

> I can only assume it may support the Mantissa and other elements
> of a floating point number, in which case, the above will not work as
> you may think..
> But who am I, I don't know anything..

I wouldn't say you don't know anything, but you don't seem to know
what a typecast does in a C program.

> stick with the "u = f;" and the compiler should be happy with that
> and call one of it's floattointeger conversion TRL functions.. But If
> you want to know how the float if constructed to represent a floating
> point number, I guess you could hack it with the cast. I think only
> the authors of the compiler would understand it's meanings..

A typecast doesn't to at all what you think it does.

--
Grant
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8 9 10
Prev: ATI Catalyst 10.3 Preview Edition
Next: Network fabric