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

Support for logical shifts #474

Open
eduardodbr opened this issue Jul 16, 2020 · 1 comment
Open

Support for logical shifts #474

eduardodbr opened this issue Jul 16, 2020 · 1 comment

Comments

@eduardodbr
Copy link
Contributor

eduardodbr commented Jul 16, 2020

The bloblang processor doesn't seem to parse the "<<"/">>" operator to do logical shifts, any ideias on how to properly do it ?

@Jeffail
Copy link
Collaborator

Jeffail commented Jul 18, 2020

So we kind of need to dig into the concept of a number here. Bloblang itself has support for both floats (64 bit) and integers (64 bit signed and also unsigned), and when two numbers are used in arithmetic it is able to make a best attempt at preserving integer based accuracy. For example, if two numbers are multiplied and both are integers then we can return an integer, if one of the numbers is a float then we cast the integer to a float and return a float.

This makes it possible to process integer values but there are also many ways of degrading them into a float. It only makes sense to add a logical shift operator if it's possible for users to know explicitly that certain values are integers of an explicit size. We also need to add documentation that clearly explains what users can expect, since most of the time the data being consumed and produced is JSON, which itself doesn't support large integers.

Originally my intention was to avoid adding bitwise arithmetic for these reasons, but if there's a strong use case for it then there are ways of getting to the point where it's possible. We would need to ensure that foo >> 1 returns a mapping error if foo is not an integer (which can be caught with (foo >> 1).catch(0)).

Before considering this we should add an optional parameter to type where the output number is replaced in favour of explicit int64, uint64 and float64, allowing you to do this if foo.type(true) == "int64" { foo >> 1 }. We should also add a method integer, which would work the same as number except always return an integer.

Finally, we'd need to audit all of the current arithmetic operators and ensure that float degradation can be avoided, right now it's a best attempt that isn't necessarily enforced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants