Prev: NET tips to expand your biz
Next: IR is insane
From: D Yuniskis on 18 Mar 2010 16:13 linnix wrote: > On Mar 18, 11:21 am, Habib Bouaziz-Viallet <h.bouazizvial...(a)free.fr> > wrote: >> Le Thu, 18 Mar 2010 10:47:44 -0700, linnix a �crit : >> >>> unsigned char u; >>> float f; >>> u = (unsigned char) f; >> In what kind of situation you need to cast a float to that unsigned >> char ??? >> If you want to extract the integer part of the float, let me tell you >> that this way is a bit silly. >> >> Question yourself before asking to C. >> >> Habib > > f is result of k1 * sine x + k2 * cosine y. > > How would you extract the integer part without casting? I thought > that's the job of the compiler. Well, technically, there are a lot of ways you *could* do it -- but most are not as efficient as a simple cast. Remember that a cast *discards* the fractional part (i.e., doesn't round). Also, the cast will misbehave if the argument doesn't fit in the data type.
From: linnix on 18 Mar 2010 16:16 On Mar 18, 12:05 pm, D Yuniskis <not.going.to...(a)seen.com> wrote: > linnix wrote: > > In response to another thread, I am still getting these problems with > > WinAVR 2010. I think the linker is hitting some AVR limits. There > > are similar reports on the web as well. > > ------------------------------------------------------------------------------------------------- > > > unsigned char u; > > float f; > > > u = (unsigned char) f; > > Approach it a little at a time: > Try casting to a (signed) int first. > If that works, then unsigned (assuming your value truly > is "non negative"). > Finally, shrink it to a char. Noop, this does not work either: int i; unsigned char u; i = (int) f; u = (unsigned char) i; --------------------------------------------------- All these ops works on x86 gcc. I guess AVR is not ready for floating points. Any one got FP apps working for AVR? For me, I am ready to ARM, as soon as my LPC (ARM Cortex M0) kit arrival. > > Sometimes compilers get upset if you do things that > it doesn't expect. > > You might also experiment with real doubles to see if > there are any subtle differences. > > > --------------------------------------------------------------------------------------------------- > > c:/wavr10/bin/../lib/gcc/avr/4.3.3/../../../../avr/lib/ > > avr35\libc.a(fp_powsodd.o): In function `__fp_powsodd': > > (.text.avr-libc.fplib+0x10): relocation truncated to fit: > > R_AVR_13_PCREL against symbol `__mulsf3' defined in .text section in > > c:/wavr10/bin/../lib/gcc/avr/4.3.3/avr35\libgcc.a(_mul_sf.o) > > c:/wavr10/bin/../lib/gcc/avr/4.3.3/../../../../avr/lib/ > > avr35\libc.a(fp_powsodd.o): In function `__fp_powsodd': > > (.text.avr-libc.fplib+0x20): relocation truncated to fit: > > R_AVR_13_PCREL against symbol `__mulsf3' defined in .text section in > > c:/wavr10/bin/../lib/gcc/avr/4.3.3/avr35\libgcc.a(_mul_sf.o)
From: Habib Bouaziz-Viallet on 18 Mar 2010 16:19 Le Thu, 18 Mar 2010 12:39:32 -0700, linnix a écrit : > > f is result of k1 * sine x + k2 * cosine y. > > How would you extract the integer part without casting? I thought > that's the job of the compiler. Sure that's the compiler job, so please cast a float to an integer (signed int). Remember char are not meant to be used for math. Keep It Simple. Habib
From: linnix on 18 Mar 2010 16:29 On Mar 18, 12:19 pm, Habib Bouaziz-Viallet <h.bouazizvial...(a)free.fr> wrote: > Le Thu, 18 Mar 2010 12:39:32 -0700, linnix a écrit : > > > > > f is result of k1 * sine x + k2 * cosine y. > > > How would you extract the integer part without casting? I thought > > that's the job of the compiler. > > Sure that's the compiler job, so please cast a float to an integer > (signed int). Remember char are not meant to be used for math. But the real world (as least in my case) is unsigned char (8 bits). Now, the strange thing is: i = (int) f; works or u = (unsigned char) i; works but not together i = (int) f; u = (unsigned char) i; Unless my apps is very unique (USB CDC A2D), I have to conclude that AVR is not ready for it.
From: Vladimir Vassilevsky on 18 Mar 2010 16:32
linnix wrote: > All these ops works on x86 gcc. I guess AVR is not ready for floating > points. Any one got FP apps working for AVR? I do heavy floating point computations with AVR (IAR workbench compiler), and it works nicely. BTW, the math on the AVR at 20MHz is somewhat 3 times faster then the same math on 40MHz PIC18. My experience is not all compilers provide for every possible cast between the standard types; I have seen bugs in float <-> int conversions also. But you can always do a function for explicit conversion. Vladimir Vassilevsky DSP and Mixed Signal Design Consultant http://www.abvolt.com |