D&C Lug - Home Page
Devon & Cornwall Linux Users' Group

[ Date Index ][ Thread Index ]
[ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] Maths! not my strong point



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

mike wrote:
|
| $binstr = '10011000111111001011100';

I think we need to know the number representation as the above is;
~      4C7E5C
not 844C7E5C

by my translation.

| $dblnum=51.1233978271484

The above code is more succintly put as

$binstr = 0b10011000111111001011100;
print $binstr/(2**18),"\n";

Giving 19.1234 (approximately) - I can add 32 if it helps.

| With rounding the answer is 51.1234

I think the mistake is to start with 32 in dblnum, and then add your
number to it. But I may have misunderstood the number representation.

| What I need to do is take 51.1234 and turn in back into a binary
string.

$decim = (2**18)*19.1234 ;
$hex = sprintf "0x%X",$decim ;
print $hex,"\n";

0x4C7E5C

Maths is suppose to be my strong point, but after 3 years of it at
university I did better on the verbal reasoning tests on my civil
service recruitment exam(?!) so there are no guarantees I'm not
blundering badly here.

Anyway be careful, Perl does double precision maths by default so
multiply or divide by 2**18 isn't THAT risky for every day numbers,
but I agree with Neil use the proper routines, or use one of the
maths shells (yacas is cool) to avoid tripping over representation
issues.
-----BEGIN PGP SIGNATURE-----
Comment: Using GnuPG with Debian - http://enigmail.mozdev.org

iD8DBQFBU0evGFXfHI9FVgYRAurMAJ4igOjtumu2bdKxyWrw0kNQ/3dOuQCfdzjL
fdQ+B0oRWpjyU083ziFxi2A=
=Sc99
-----END PGP SIGNATURE-----

--
The Mailing List for the Devon & Cornwall LUG
Mail majordomo@xxxxxxxxxxxx with "unsubscribe list" in the
message body to unsubscribe.


Lynx friendly