When should I use the bignumber library? [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to understand when to use the bignumber library.
Question 1). As we know, JavaScript has an upper limitation on the Number type which is 2^53, but it also has another type, BigInt, which doesn't have a limit. Why would we use the bignumber library? Is it because it also works with decimals? Are there any other reasons?
Question 2) Let's say I have a x = new BigNumber(10000000000000000000); and now I want to multiply this by 30. Should I also transform 30 to BigNumber first and then multiply or would this be okay: 30 * x? It seems like that I didn't transform 30 to BigNumber and it still works correctly. When should I use mul from the bignumber library?

Why would we use bignumber library? is it because it also works with decimals?
Yes. A bignumber library typically works with arbitrary-precision floating-point numbers, while a bigint library (or also the builtin data type) can only handle integers.
In addition, there are quite a few libraries that implement big integer math which came into existence before the native support of BigInt in JavaScript, a rather recent addition to the language. This also means a library is necessary in older browsers.
Now I want to multiply this by 30. Should I also transform 30 to bigNumber first?
No, this is usually not necessary (but of course depends on the library).
would this would be okay: 30 * x?
No, that would not be ok. If you're working with a bignumer library, you will need to use its methods (like x.multiply(30)), not the * operator which only works with JavaScript's builtin data types.

Related

Loading lookup table from server - Efficient Format [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
If I had a python script that created a lookup table which could be read by a webpage (javascript and maybe ajax), what is the most efficient (in speed and if possible size) format to use?
The lookup-table could have 2000 rows.
Here is a data example:
Apple: 3fd4
Orange: 1230
Banana: 942a
...
Even though this is primarily opinion based, I'd like to roughly explain to you what your options are.
If size is truly critical, consider a binary format. You could even write your own!
With the data size you are presenting, we are probably talking megabytes of data (depending on the field values and no. of columns), so the format is of importance. Now, a simple csv or plain text file - provided it can be read by the webpage - is very efficient in terms of the additional overhead: simply seperating the values by a comma and putting the table headers on line 1 is very, very concise.
JSON would work too, but does maintain a somewhat larger overhead than just a raw (text) data dump (like a csv would be). JavaScript object notation is often used for data transfers, but really, in the case of raw data it does not make much sense to coerce it into such a format.
Final thoughts: put it into a relational database and do not worry about it any more. That is the tried and tested approach to any relational data set, and I do not really see a reason you should deviate from that format.

Errors in javascript programs [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is there a good classification of standard javascript errors? For example, in java like programs, there are errors like ArrayIndexOutOfRange, resource leaks, race conditions etc.
Also, in Javascript few errors are not reported as exceptions (e.g., divide by zero). Are there any other similar behaviors that are not reported as runtime exceptions in javascript?
MDN has a great article about this, they can put it better than I ever could:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Error_types
Also, good classification here in the ECMA standard:
ECMAScript 5.1 (Current): http://www.ecma-international.org/ecma-262/5.1/#sec-15.11.6
ECMAScript 6 (Coming soon, some features already here in certain browsers): http://www.ecma-international.org/ecma-262/5.1/#sec-15.11.6
In terms of "not being reported as runtime errors", there are some evaluations of expressions that do not halt execution of code but return indicators like NaN, e.g.:
var a = "Hello";
var b = 3;
var c = a / b; // c is "NaN"
You can use the isNaN() function to check for this. Unfortunately I don't know of an offical definite list of these scenarios (if there are more) or even how you would classify them. I guess it comes down to experience and learning the features (or quirks depending on your perspective!) of the language.

What are the pros of loose typing? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
JavaScript is said to be a "loosely-typed" language. This is due to the fact that the runtime allows operations to be performed on operands of different types (via coercion):
var number = 6;
var bool = true;
var result = number + bool; //result is 7
Coming from a mostly statically-typed, strongly-typed background, I am having a hard time reasoning about the benefits of this type of approach. Sure, it can make for some pretty concise syntax, but it also seems like it could cause a nightmare when trying to track down bugs. So, besides conciseness, what are some of the benefits of loose typing and implicit type conversions?
Loosely typed languages have a number of differences which can be taken as advantages:
There is no need of interfaces. As long as an object has the method name that you need, call that method. Not using interfaces can simplify coding and reduce code size.
There is no need of generics, for very similar reasons.
"by type" function overloads are handled more simply If a function needs a string parameter, then just cast the incoming value to a string. If type checking is needed, it can be added there.
We don't have, or need, classes. The [almost] everything is an object makes passing values around much easier. No need to auto-box, no need to cast values coming out.
Objects are easily extended without breaking code. You can create an array then drop replace the indexOf method to use one uses the binary search. The end result is smaller, and IMHO, cleaner code.

How can I make my JavaScript program calculate more digits of e? [duplicate]

This question already has an answer here:
What is the standard solution in JavaScript for handling big numbers (BigNum)?
(1 answer)
Closed 8 years ago.
I'm looking for a Mathematical solution that deals with really (long, big, huge, storms) numbers. I haven't found anything yet, But I don't wanna think that this problem hasn't be solve at this time. I'm looking for an easy Number solution, like Microsoft Excel Precision (30 decimals), or a BigInteger (Java) solution. in Javascript of course.
While looking for an big integer library for an ElGamal crypto implementation I tested several libraries with the following results:
I recommend this one: Tom Wu's jsbn.js (http://www-cs-students.stanford.edu/~tjw/jsbn/)
Comprehensive set of functions and fast
Leemon Baird's big integer library (http://www.leemon.com/crypto/BigInt.js)
Comprehensive set of functions and pretty fast
BUT: Negative number representation is buggy!
bignumber.js (https://github.com/MikeMcl/bignumber.js)
Pretty complete set of functions
BUT: Converting really big numbers from strings into BigNumber objects result in INFINITY
Scheme arithmetic library for JavaScript (https://github.com/jtobey/javascript-bignum)
JS-Implementation of Scheme arithmetic functions
BUT: No function for y= x^e mod n
I haven't tested this by myself: BigNumber (http://jsfromhell.com/classes/bignumber)
Functions for high precision claculations
BUT: It's said to be slow due to internal representation of numbers as strings
There's a BigInteger library for JavaScript available here:
jsbn.js
(Note that I haven't used this myself. Try it and see what you think.)
There is also Silent Matt's library for Big Integers. It does not handle decimals.

Conversion from JavaScript to Python code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Is there a relatively quick program out there to accomplish at least the basics of this? Just a few regexes? I'm willing to do some manual conversion, but this is a pretty big set of scripts.
You can translate JavaScript to Python using Js2Py. It supports whole JavaScript and you can use it to translate large JavaScript modules like esprima.js (a JavaScript 6 parser).
Short demo:
>>> import js2py
>>> f = js2py.eval_js( "function $(a) {return a + arguments[1]}" )
>>> f
function $(a) { [python code] }
>>> f(1, 2, 3)
3
This is how the translated function looks like internally (it's rather ugly):
>>> print js2py.translate_js( "function $(a) {return a + arguments[1]}" )
from js2py.pyjs import *
var = Scope( JS_BUILTINS )
set_global_object(var)
# Code follows:
var.registers([u'$'])
#Js
def PyJsHoistedNonPyName(a, this, arguments, var=var):
var = Scope({u'a':a, u'this':this, u'arguments':arguments}, var)
var.registers([u'a'])
return (var.get(u'a')+var.get(u'arguments').get(u'1'))
PyJsHoistedNonPyName.func_name = u'$'
var.put(u'$', PyJsHoistedNonPyName)
Updated
Now several (4) years later this (almost certainly) can be done; though certainly not with RegEx.
I suggest future readers look to #Piotr Dabkowski's answer..
Or some of the other answers. (I don't know having not tried them)
Original Answer
Hm this is a hard one.
The definition of a compiler is translates from a higher level language to a lower level language.
eg python to machine-code.
or java to javascript (google has a rather famous compiler for this somewhere - its' what makes google doc easier to make)
Python to javascript compilers abound.
technically javascript to python would be a decompiler. (afaik)
I found some speculation about a javascript-python converter here: follow the tread through. it mostly speaks of how it wouldn't be too hard to do.
I can't find anything , but that doesn't mean it's no out there.
Regex is not suitable, regex is suitable only for regular languages.
programming languages are not normally regular languages. see this
This answer might be about 2 years late but how about js -> CoffeeScript -> python? If you use something like http://js2.coffee/ to convert to cs, then things like indentation and lists are already done for you.
If what you are asking is converting a few regexs in javascript to Python equivalent, the basics of regular expressions are mostly pretty standard. check out the Python re module doc. See if what you are using is documented. Also of interest to you is this page.
If you are talking about converting javascript code to Python equivalent, the best converter is you. Learn about Python, and then convert them manually. there is nothing else better than the human being. Programming constructs like loops, variables, arrays are pretty common and standard you will recognize instantly how to use them straight away.

Categories