Emulating Int64 Overflows in Ruby

Emulating int64 overflows in Ruby

64-bit integers are represented in Ruby MRI as Bignums internally even on 64-bit platforms in some cases (due to implementation details, Fixnums are only 63 bit long on 64-bit platform sand 31 bit long on 32-bit platforms). Hence, it will be much more faster to use binary "and" operator &:

ruby-1.9.2-p290 :001 > a = 2**128 + 1256231
=> 340282366920938463463374607431769467687
ruby-1.9.2-p290 :002 > a & (2 ** 64 - 1)
=> 1256231
ruby-1.9.2-p290 :003 > a & 0xffffffffffffffff
=> 1256231

The last variant is a bit uglier, but also faster, too, as Ruby MRI lacks constant folders. If you'd do the 002 clause in a loop, it would compute 2**64 - 1 each time).

Ruby MRI is an official ("Matz Ruby Implementation") variant of Ruby, i.e. "plain" Ruby which most of us use. The details I've listed here may or may not apply this way to other implementations, but binary "and" is generally faster or as fast as modulo operator on any platform or language.

Algorithm for deviations

It's a very easy thing to calculate, but you will need to tune one parameter. You want to know if any given value is X standard deviations from the mean. To figure this out, calculate the standard deviation (see Wikipedia), then compare each value's deviation abs(mean - value) from the mean to this value. If a value's deviation is say, more than two standard deviations from the mean, flag it.

Edit:

To track deviations by weekday, keep an array of integers, one for each day. Every time you encounter a deviation, increment that day's counter by one. You could also use doubles and instead maintain a percentage of deviations for that day (num_friday_deviations/num_fridays) for example.

Which scripting languages support long (64 bit) integers well?

The size of high speed hardware integers (assuming the language has them) will always be dependent on whatever size integers are available to the compiler that compiled the language interpreter (usually C).

If you need cross-platform / cross-version big integer support, the Perl pragma use bigint; will do the trick. If you need more control, bigint is a wrapper around the module Math::BigInt.

In the scope where use bigint; is loaded, all of the integers in that scope will be transparently upgraded to Math::BigInt numbers. Lastly, when using any sort of big number library, be sure to not use tricks like 9**9**9 to get infinity, because you might be waiting a while :)

The maximum value for an int type in Go

https://groups.google.com/group/golang-nuts/msg/71c307e4d73024ce?pli=1

The germane part:

Since integer types use two's complement arithmetic, you can infer the
min/max constant values for int and uint. For example,

const MaxUint = ^uint(0) 
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1

As per @CarelZA's comment:

uint8  : 0 to 255 
uint16 : 0 to 65535
uint32 : 0 to 4294967295
uint64 : 0 to 18446744073709551615
int8 : -128 to 127
int16 : -32768 to 32767
int32 : -2147483648 to 2147483647
int64 : -9223372036854775808 to 9223372036854775807

Julia: Instantiated type parameters

It is currently not possible to restrict type parameters like this, though there has been discussion to allow the syntax that you tried up at the top. I believe that the solution that you came up with of checking the type parameter in an inner constructor is considered the best practice right now.

time.Sub() returns 1 second despite the difference exceeding couple of years

go's time package uses both "wall clock" (what you are trying to change) and a monotonic clock. From the docs:

Operating systems provide both a “wall clock,” which is subject to
changes for clock synchronization, and a “monotonic clock,” which is
not. The general rule is that the wall clock is for telling time and
the monotonic clock is for measuring time. Rather than split the API,
in this package the Time returned by time.Now contains both a wall
clock reading and a monotonic clock reading; later time-telling
operations use the wall clock reading, but later time-measuring
operations, specifically comparisons and subtractions, use the
monotonic clock reading.

[...]

If Times t and u both contain monotonic clock readings, the operations t.After(u), t.Before(u), t.Equal(u), and t.Sub(u) are carried out using the monotonic clock readings alone, ignoring the wall clock readings.

This is specifically designed to prevent deviant app behavior when a clock-sync (ntp etc.) occurs (and pushes the clock back). go's time package ensures the monotonic clock reading always moves forward (when comparing or subtraction operations).



Related Topics



Leave a reply



Submit