Skip to content

Conversation

@zebreus
Copy link

@zebreus zebreus commented Aug 6, 2022

This adds support for parsing positive signed numbers like +1.

@zebreus
Copy link
Author

zebreus commented Aug 21, 2022

@MikeMcl Have you had a chance to look at the changes yet?

I use big.js to parse numbers from CSS stylesheets, but the CSS spec explicitly allows a positive sign. If that would be supported I can use it to directly parse the numbers from my stylesheets.

@johnmanko
Copy link

Although I don't have a direct need for this capability, it makes sense. The - prefix is allowed, and so should +.

@MikeMcl
Copy link
Owner

MikeMcl commented Aug 21, 2022

See #59, #70 and #134.

It's a reasonable suggestion, but I find it a little strange that you have gone straight to a PR when support for a leading plus sign is not likely to have been something I hadn't thought of previously but rather something that was deliberately not included.

I am reluctant to accept this because it's another if statement in a very hot path and it's very easy for users who want to strip the plus sign to do it themselves anyway. What next? Support for leading whitespace? What about e.g. "+ 1"?

One possibility is a Big.from or Big.parse method that is more liberal than the Big constructor. That could also allow underscores as in e.g. "1_000_000".

@zebreus
Copy link
Author

zebreus commented Aug 23, 2022

Sorry, I hadn't looked through the closed issues. Should have done that before opening the PR. I assumed that a leading plus sign just wasn't a feature that many people required and nobody was missing it.

I totally agree to keep the number parsing scope of big.js to a minimum, after all it's a library for calculating numbers and not for parsing numbers. I think a well defined scope would be to support any decimal number string in a format that is also supported by the Number constructor, but nothing more. That is probably also what most users expect. It would also define that NumericLiteralSeparator and spaces are invalid, so there is no need to add support for those.

The performance impacts of adding support for a leading plus sign are basically zero. I did some benchmarking and could not find any significant performance difference between supporting plus or not.

Detailed benchmark results

I tested the performance of the Big function with the following test function:

const test = (value) => {
  const a = new Date()
  for(var i = 0; i < 1e8; ++i) { Big(value) }
  const b = new Date()
  return b.getTime() - a.getTime()
}

I tested my fork against the current master with three different values

value time without plus support time with plus support
8 8325ms 8186ms
-998 10663ms 10585ms
845439825732e67 19194ms 19254ms

I think there are enough users who want a Big constructor that is more analogous to the Number constructor, to warrant a single line of code more.

@MikeMcl
Copy link
Owner

MikeMcl commented Aug 23, 2022

I assumed that a leading plus sign just wasn't a feature that many people required and nobody was missing it.

I agree with your assumption that it isn't a feature that many people require.

I think there are enough users who want a Big constructor that is more analogous to the Number constructor, to warrant a single line of code more.

Hmmm... this seems to contradict your above assumption somewhat and your PR involves more than adding a single line.

I think a well defined scope would be to support any decimal number string in a format that is also supported by the Number constructor, but nothing more... It would also define that... spaces are invalid,

Well, we can have, for example

Number('  \n   +2  \t\n  ')    // 2

so I suppose you mean whatever is supported by Number apart from leading and trailing whitespace characters.

Other arguments for are that a leading plus sign is supported by sister libraries decimal.js and bignumber.js, and also by Java's BigDecimal and Python's Decimal, and that this library already supports the similarly redundant plus sign for exponents.

Big('1e+2').toString();    // '100'

It's a borderline decision (that has already been made and remade previously) and in such cases here I usually follow the maxim: 'If in doubt, leave it out'.

Anyway, thanks for your efforts. I may include this but I'm not deciding the matter today.

@zebreus
Copy link
Author

zebreus commented Sep 13, 2022

Closing this PR, because it probably does not fit into big.js. I have published this as @zebreus/big.js to npm, if anyone else needs support for plus signs.

Thanks for the creating this library, I really enjoy using it.

@zebreus zebreus closed this Sep 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants