Prev: HLA wont compile!!
Next: Structs in Assembly
From: ?a/b on 16 Oct 2005 03:36 On Sat, 15 Oct 2005 08:19:23 +0200, "?a\\/b" <al(a)f.g> wrote: this is for represent number in "fixed point" type >if i have the decimal number is string format > >"-1234568788887877.0000839393948450154454454" > >then integer_number=-1234568788887877 > >i have the fractional part of the number e.g. > s ="0.0000839393948450154454454" >if r = 839393948450154454454; like big integer >the "formula" *seems* to be this: > >precision= precision_in_bits_of_fractional_part > > (precision) > 2 * r >Fraction_number = [-------------------------------------] > (strlen(s)-2) > 10 > >(where [] means iteger part) >so the number for conversion is: > >if( Fraction_number > Max_precision_number ) > {k=len_in_bits(Max_precision) - len_in_bits(Fraction_number); > Fraction_number >>= k; > } Is there someone know if *always* here Fraction_number <= Max_precision_number? If so why? >if (integer_number>0) > number = (integer_number<<precision) + Fraction_number >else number = (integer_number<<precision) - Fraction_number > >and is very easy and seems very well
From: Richard Cooper on 16 Oct 2005 04:23
On Sun, 16 Oct 2005 03:36:58 -0400, ?a\/b <al(a)f.g> wrote: >> if( Fraction_number > Max_precision_number ) >> {k=len_in_bits(Max_precision) - len_in_bits(Fraction_number); >> Fraction_number >>= k; >> } > > Is there someone know if *always* here > Fraction_number <= Max_precision_number? If so why? Hmm, I totally missed that little piece of code, otherwise I would have said something. I assume that Fraction_number is the result of the division, viewed as an integer, and Max_precision_number is 2 ^ precision (or maybe 2 ^ precision - 1). If that's true, then yes, Fraction_number will always be <= Max_precision_number. The reason is...well... You've got 0.xxxx, which you put through this formula: xxxx * 2 ^ p ----------------- 10 ^ length(xxxx) (where p is precision, just shorter) Using a bit of 8th grade math, we get this: xxxx ----------------- * 2 ^ p 10 ^ length(xxxx) Now 10 ^ length(xxxx) is always going to be larger than xxxx, so the limit of that fraction is 1. This limits the result of the entire equation to 2 ^ p. Basically, the equation always gives you the correct bit pattern, and you never need to shift the result. |