[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Javascript comparissons cause overflow

JT

4/4/2015 7:35:00 AM

After i managed to write some functions actually doing arithmetic in bignumbers, basicly by handling them as strings where digit places read into an array.

I decided to try change a previously written basechanger into also handling bignumbs. And it proved to be a much harder task than i origially thought.


How do you go from compare size of integers to compare size of stored digitplaces in arrays?

Also the bignumb multiplication assume that the two arrays passed to it is already parsed in the order bigger, smaller. Should i really have to move the parsing code into the multiplication, or make a function call from multiplication changing the order?

I had not expect much of the code needed to be changed, except for the actual arithmetic operands with operators, interchanged with function calls. But it is alot more things like comparissons that would need to change.
3 Answers

JT

4/4/2015 7:48:00 AM

0

Den lördag 4 april 2015 kl. 09:35:04 UTC+2 skrev jonas.t...@gmail.com:
> After i managed to write some functions actually doing arithmetic in bignumbers, basicly by handling them as strings where digit places read into an array.
>
> I decided to try change a previously written basechanger into also handling bignumbs. And it proved to be a much harder task than i origially thought.
>
>
> How do you go from compare size of integers to compare size of stored digitplaces in arrays?
>
> Also the bignumb multiplication assume that the two arrays passed to it is already parsed in the order bigger, smaller. Should i really have to move the parsing code into the multiplication, or make a function call from multiplication changing the order?
>
> I had not expect much of the code needed to be changed, except for the actual arithmetic operands with operators, interchanged with function calls. But it is alot more things like comparissons that would need to change.

This is probably why bignumb should be supported right out of the box in programming languages. Because it is not just the actual code dealing with the arithmetic operators that has to change, the whole approach when writing code has to change, since every comparisson is affected (well at least though incorporating possible bignumbs).

Ben Bacarisse

4/4/2015 3:01:00 PM

0

jonas.thornvall@gmail.com writes:

> Den lördag 4 april 2015 kl. 09:35:04 UTC+2 skrev jonas.t...@gmail.com:
>> After i managed to write some functions actually doing arithmetic in
>> bignumbers, basicly by handling them as strings where digit places
>> read into an array.
>>
>> I decided to try change a previously written basechanger into also
>> handling bignumbs. And it proved to be a much harder task than i
>> origially thought.
>>
>> How do you go from compare size of integers to compare size of
>> stored digitplaces in arrays?

I can't interpret this question. Comparing bignums should not be hard.

<snip>
> This is probably why bignumb should be supported right out of the box
> in programming languages.

Many languages have unbounded integers and/or some other form of bignum
built-in (even more have them as a library extension but that can be
less convenient). Is there a reason you have to use javascript?

<snip>
--
Ben.

John Harris

4/4/2015 4:45:00 PM

0

On Sat, 4 Apr 2015 00:34:59 -0700 (PDT), jonas.thornvall@gmail.com
wrote:

<snip>
>How do you go from compare size of integers to compare size of stored digitplaces in arrays?
<snip>

You write a compare function that returns
-1 if a < b
0 if a == b
+1 if a > b

Then you can write the boolean comparison functions if you need them.
a < b if compare == -1
a <= b if compare == -1 or == 0
etc.

Something to remember is that the computer does this in between clock
beats; software takes a lot longer, even in bignumb implementations.

John