release
dev builds
extras
themes manual
wiki
device status forums
mailing lists
IRC bugs
patches
dev guide

# Rockbox mail archive

Subject: Re: math

# Re: math

From: Dave Wiard <lists_at_wiard.net>
Date: 2005-07-06

Ray Lambert wrote:
> BlueChip wrote:
>
>> while (x>0) r = (r<<3)+(r<<1); // r*8 + r*2
>> ^whoops
>>
>> what values of 'x' ? all positive? include 0?
>>
>> if (x<0) return(0); // cannot handle negative values of x
>> if (x>9) return(-1); // result will be > (2^32)-1
>> unsigned long r = 1;
>> while (x>0) r = (r<<4)+(r<<1); // r*8 + r*2
>> return (r);
>
> It might be a good idea to decrement x at some point too.... :)
>
>> while (x>0) r = (r<<3)+(r<<1); // r*8 + r*2
>
> perhaps:
>
> for (; x>0; --x) r = (r<<3)+(r<<1); // r*8 + r*2
>
> ~ray

And catch x=0. And set your base at 10 not 1 (remember, this is exp10).

if (x == 0) return 1; // 10^0 is always 1
if (x < 0) return 0; // cannot handle negative values of x
if (x > 9) return -1; // result will be > (2^32)-1
unsigned long r = 10;
while (--x) r = (r << 3) + (r << 1); // r*8 + r*2
return r;

Dave
_______________________________________________
http://cool.haxx.se/mailman/listinfo/rockbox
Received on Wed Jul 6 22:07:31 2005