JavaScript Notes: Precision Woes

The problem is "a lack of precision"

While JavaScript is quickly becoming the lingua franca of the web, this general purpose language contains built-in limitations which restrict its use to simple applications. For example, I recently (2012) attempted to write a Diffie-Hellman Key-Exchange Demo in JavaScript but detected problems during testing which I traced to a lack of mathematical precision. Check out these comparisons:

JavaScript Math.pow(7,18) 1,628,413,597,910,449 correct
Calculator 7^18 1,628,413,597,910,449 correct
JavaScript Math.pow(7,19) 11,398,895,185,373,144 incorrect
Calculator 7^19 11,398,895,185,373,143 correct
JavaScript Math.pow(7,20) 79,792,266,297,612,000 incorrect
Calculator 7^20 79,792,266,297,612,001 correct
JavaScript Math.pow(7,21) 558,545,864,083,284,030 incorrect
Calculator 7^21 558,545,864,083,284,007 correct

PERL Workaround

Legend:	<sr> = system response
	<ur> = user response

<sr>	$
<ur>	type
	#       title:
	use bigint;             # use bigint after this point
	$x = 7 ** 19;           # x = 7^19
	printf("%s\n",$x);      # "%s" because other formats bomb
<sr>	$
<ur>	perl
<sr>	11398895185373143	! correct
<ur>	$

Back to Home
Neil Rieck
Waterloo, Ontario, Canada.