Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detecting overflows... #20

Closed
jcalfee opened this issue Jun 13, 2015 · 6 comments
Closed

Detecting overflows... #20

jcalfee opened this issue Jun 13, 2015 · 6 comments

Comments

@jcalfee
Copy link

jcalfee commented Jun 13, 2015

I noticed that an overflow may not be as straight forward as detecting one in JavaScript (via MAX_SAFE_INTEGER). For example, the resulting value falls in range with respect to Long.MAX_VALUE or Long.MAX_UNSIGNED_VALUE

> Long.MAX_VALUE.toString()
'9223372036854775807'
> Long.fromString("288888888888888888888888888888").toString()
'236889850951994936'

> Long.MIN_VALUE.toString()
'-9223372036854775808'
> Long.fromString("-288888888888888888888888888888").toString()
'-236889850951994936'

So, how would one detect an overflow?

As a work-around, what do you think of this?

coffee> is_long_overflow = (string_value)-> Long.fromString(string_value).toString() != string_value
> is_long_overflow "9223372036854775807"
false
> is_long_overflow "9223372036854775808"
true
> is_long_overflow "-9223372036854775808"
false
> is_long_overflow "-9223372036854775809"
true
@dcodeIO
Copy link
Owner

dcodeIO commented Jun 13, 2015

Your solution looks OK to me. There is no analogy between Number.MAX_SAFE_INTEGER and Long.js though, as this constant rather indicates precision loss for greater magnitudes due to JavaScript numbers being doubles internally.

@jcalfee
Copy link
Author

jcalfee commented Jun 13, 2015

How to know if it overflows after it is a Long object?

> Long.fromString("0").add(Long.MAX_VALUE).toString()
'9223372036854775807'
> Long.fromString("1").add(Long.MAX_VALUE).toString()
'-9223372036854775808'

@dcodeIO
Copy link
Owner

dcodeIO commented Jun 13, 2015

It overflows like any other (64 bit) two's complement integer.

@jcalfee
Copy link
Author

jcalfee commented Jun 15, 2015

Interesting ... I'm guessing here, but I'm starting to think that overflows may be a case-by-case scenario. If so this may mean that Long.js is ideal for ByteBuffer but that I may need to go to a larger number library like bn.js for what I'm trying to do. In this way, I can overflow 64 bits getting the correct answer but checking my final result to see if it exceeds the 64 bits. I believe you mentioned bn.js. Is that not over-kill though for 64 bits? Do you think your library will be more efficient for these?

@dcodeIO
Copy link
Owner

dcodeIO commented Jun 15, 2015

Unfortunately, I am not familiar with the bn.js codebase and don't know how efficient it is, but it might be an option if you need to work with more than exactly 64 bits of information (or a change of sign is not a good enough indicator for an overflow).

@jcalfee
Copy link
Author

jcalfee commented Jun 15, 2015

Ok, so "not supported" ... I think, if supported, overflow detection would need to be internal to the library and performed on all operations. I will use a large number library instead so I can work it out on my side. Thank you...

@jcalfee jcalfee closed this as completed Jun 15, 2015
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

No branches or pull requests

2 participants