replicate javascript unsafe numbers in golang [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
So in go, I do a calculation which gives "5726718050568503296"
In JS it gives "5726718050568503000"
I would like to replicate this behavior in go

As discussed in the comments of Why is 5726718050568503296 truncated in JS as others have mentioned, this is caused by the way toString() is implemented in JavaScript which appears to use the minimium number of significant digits to return the same represented number, rather than returning the mathematically closest number. You can however replicate this behaviour in Go by using the special -1 precision with strconv.FormatFloat:
package main
import (
"fmt"
"strconv"
)
func main() {
n, _ := strconv.ParseInt(strconv.FormatFloat(5726718050568503296.0, 'f', -1, 64), 10, 64)
fmt.Println(n)
}
Playground link: https://play.golang.org/p/9ZObcB3so4o

Seems like there is no easy way around it.
JS has its way of representing the Number type and the integer you presented basically overflows the capacity of it.
Please refer to: https://stackoverflow.com/a/1379973/10316247 for more information about how JS handles this and why this wierd behavior happens.
Replicate that in go would be very very trick as you'd need to look at the byte representation of this int and try to assume how JS would handle it.
I would (as the link above suggests) use it as and string.

Related

What algorithm does babel use to convert a simple div into x.createElement('div')? [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 3 years ago.
Improve this question
I am curious how babel's algorithm works for converting JSX into
x.createElement(
type,
[props],
[...children]
)
Can anyone explain how babel works for a simple JSX div?
<div id="hello-world" > Hello World </div>
Babel is a compiler. It's not that simple.
Most compilers break down into three primary stages: Parsing, Transformation, and Code Generation
Parsing is taking raw code and turning it into a more abstract representation of the code. (Parsing working example: here. Go ahead, use your <div> example.)
Transformation takes this abstract representation and manipulates to do whatever the compiler wants it to.
Code Generation takes the transformed representation of the code and turns it into new code.
For a quick tutorial on compilers, see this.
Another great resource on Babel internal working. here.

is javascript Math.sin() O(1)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am wondering if Math.sin(), Math.cos() are O(1), and how is it translated to the compiler.
We can create a match table for it.
Or we can create a not so detailed match table and use a linear formula to calculate the value.
Or we can break it down.
I guess in any sense Math.sin() is sorta O(1) as the input is bounded, but how exactly is it done in the compiler and how complex it is?
It might be impossible to give a definite answer, so I will try to provide you with as much information as I can instead.
The Math object implementations can differ between browsers and operating systems ("Note that many math functions have a precision that's implementation-dependent. This means that different browsers can give a different result, and even the same JS engine on a different OS or architecture can give different results.[1]").
There are a few major ways in which a sine function can be implemented:
Taylor series approximation
CORDIC algorithm
Complexity will depend on implementation. As far as I know, CORDIC algorithm is more common.

Math equation validation regex for javascript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to have a validation for my Math equations
Let us assume I have equations as follows:
1) x + y = e // valid equation
2) x + sin(x) = y //valid equation
3) a + wq = c // invalid equation
4) sin(x) + s =y //invalid equation // as s is not wanted only x,y,e characters are allowed to type in
I only want to allow equations which may contain x,y any other special character like e , also the functions like sin,cos,tan,sec,csc,cot,arcsin,asrccos,arctan,arcsec,arccot etc.
But if the equation contains a single letter like the above equation then the equation is not valid.
So can anybody provide me with a Math Regex that would help me validate?
Thank in advance!!
I had the same thing happen to me. The content security policy prevents the ChromeExtension from being able to access the GLOBALS array, which make sense. The Chrome Extension ideally should be sandboxed, as Chrome Extensions have extended permissions.
That being said this is the wild west AND gmail.js is an unsupported JS library that could change with changes in the Gmail code, so you are already assuming a fair amount of risk by using it.
It is possible to inject the Gmail.js code into the Gmail application and instructions are outlined here
https://github.com/KartikTalwar/gmail-chrome-extension-boilerplate
Use the structure here and you should be able to access the GLOBALS array within GMail.

How do you multiply negative numbers in javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This may sound stupid, but if you are trying to do something like 8*-5 (eight times negative five), you can't exactly.
In order of operations, you could do something like this: 8*(-5). But in javascript (and in most other languages), parentheses are the condition symbol.
Please help.
You can use both code in the javascript.
8*-5 == -40
8*(-5) == -40
As multiply symbol is greater precedence than minus symbol, it will be multiplied in normal math and do so in JavaScript.
if you are trying to do something like 8*-5 (eight times negative five), you can't exactly.
Of course you can. Open your JavaScript console and type 8*-5. You get -40.
In order of operations, you could do something like this: 8*(-5). But in javascript (and in most other languages), parentheses are the condition symbol.
Not sure what gave you that idea, but parenthesis can be used just fine in math operations and for setting order of operations. Again, open your JavaScript console, type 8*(-5) and you will see -40.

Is there a good way to identify a word, even if it's misspelled in javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a bunch of words (mostly locations, like Stockholm and London) and a bunch of strings. like "I want to go from stockholm to london". I want to find out what words are in what strings.
I currently use .indexOf to perform this task.
Is there a library, method, function etc, in javascript that identifies misspelled versions of words?
https://github.com/epeli/underscore.string#readme
Check out the levenshtein _.levenshtein(string1, string2) distance function. It can be used to calculate the distance between too strings.
I found this JS library http://www.javascriptspellcheck.com/ that is supposed to check spelling in several languages
After reading the above I'm not sure if I'm exactly clear on what you are trying to do... but regarding your final question about identifying words which are incorrectly spelled -- I'd take a look at java script spell check. And as a side note.. Often people try and use soundex when they want to count words which are spelled both correctly and incorrectly.
php by default has levenshtein function. you can use this method by using PHPJS library.
by finding the levenshtein distance between the word and a dictionary of correct words you can obtain a word with least levenshtein distance from the miss-spelled word. This would most probably be the correct spelling for a particular word.
In the past, I have used MissPlete library, which uses Jaro–Winkler distance algoritm by default.
It has no dependencies (not even jQuery), which I highly value.

Categories