Utilities for handling math and showing numbers in Java.

Overview

digital

Utilities for handling math and showing numbers in Java.

What is it?

BitConversion allows converting float and double values to int and long versions of their underlying bits (and it does this in a way that works on GWT efficiently). It also has some methods that convert between float and int with reversed byte order (using a fast intrinsic on desktop JDKs and a special trick on GWT), and others that get only the low or high half of a double's bits as an int. It is modeled after the NumberUtils class from libGDX, but offers extra methods.

Base is much larger, and allows converting any Java primitive number type to a specific base/radix/number-system. Here, Bases are flexible enough to be able to be generated randomly for obfuscation purposes (such as to mangle a high score in a save file). There are several Base constants already defined. Each one can write numbers as signed (variable-length) or unsigned (fixed-length), as well as read back either type from one method. Parsing is significantly more relaxed than in the JDK, and invalid numbers tend to be returned as 0 rather than requiring an Exception to be caught.

TrigTools and MathTools provide various mathematical functions. On one hand, TrigTools tries to be as complete as possible, offering sin, cos, tan, asin, acos, atan, and atan2 in radians, degrees, and turns. On the other hand, MathTools offers a wild grab bag of math functions and constants, from simple lerp, floor, ceil, and clamp methods to an optimized cube root function and a parameterized spline.

ArrayTools provides common code for dealing with 2D arrays, and also sometimes 1D arrays. It allows copying, inserting, and filling 2D arrays, and creating ranges of 1D arrays.

Hasher is... large. It provides fast, high-quality hashing functions for primitive arrays (and arrays of objects, if they implement hashCode()), and has 64-bit and 32-bit variants. The specific hashing algorithm it uses is a somewhat-hardened version of wyhash that doesn't use 128-bit math. It also has a few unary hashes that can be used as quick and dirty random number generators when applied to numbers in a sequence.

How do I get it?

With Gradle, add this to your dependencies (in your core module, for libGDX projects):

api "com.github.tommyettinger:digital:0.0.2"

If you target GWT using libGDX, you will also need this in your html module:

api "com.github.tommyettinger:digital:0.0.2:sources"

and this in your GdxDefinition.gwt.xml file:

<inherits name="digital" />

You can also use JitPack to get a recent commit; in that case, follow its instructions here.

License

Apache 2.0. This includes some modified code from libGDX, SquidLib, SquidSquad, Uncommon Maths, and wyhash.

Comments
  • Add Fibonacci formula

    Add Fibonacci formula

    Might need additional clarification in the documentation for which values are considered "too large".

    Might also need further testing in case of any unknown issues involving casting doubles to ints.

    opened by jmcwilliams403 2
  • Add invSqrt

    Add invSqrt

    This is the original Quake III Arena implementation of Fast InvSqrt() from id Tech 3, ported to Java using Float.floatToIntBits() and Double.doubleToLongBits(). May need to add a disclaimer that any observable performance difference over 1/Math.sqrt(x) is entirely system-dependent

    opened by jmcwilliams403 2
  • Added float/double literals for sqrt 2/3/5

    Added float/double literals for sqrt 2/3/5

    I originally considered of putting root 2 and root 3 in trigTools and then root 5 in math tools, but I decided it would have probably made the organization needlessly confusing; However, in the documentation I did point out that the former two have useful properties in trigonometry, so I'll leave it open to further judgement.

    opened by jmcwilliams403 1
  • Added PHI and PSI aliases for GOLDEN_RATIO

    Added PHI and PSI aliases for GOLDEN_RATIO

    PHI is the greek letter often assigned to the Golden Ratio (especially in Fibonacci-esque functions) while PSI is equivalent to the negative GOLDEN_RATIO_INVERSE, or the conjugate of PHI (i.e. PHI^-1, -1/PHI, or 1-PHI, etc.) Explanation on Wikipedia

    opened by jmcwilliams403 1
  • Added shuffle functions

    Added shuffle functions

    based on Knuth shuffle. May need to add clarification in comments for where this algorithm came from.

    Seeds are given as capital Long object wrappers to allow null seeds to indicate unseeded randomness.

    opened by jmcwilliams403 1
  • Include double version of PI for parity

    Include double version of PI for parity

    with double values locally defined for all variations of pi, the developer would not need to awkwardly switch back and forth between writing static references to TrigUtils and to java.Math

    opened by jmcwilliams403 1
  • Add nth root function

    Add nth root function

    Documentation is very rough and I would recommend additional clarification all around.

    Also I'm not sure how essential the extra check afterwords really is, so maybe some tests can be done with and without it.

    opened by jmcwilliams403 0
  • use precise hex float value for CEIL

    use precise hex float value for CEIL

    this value is equivalent to tacking an extra 4 at the end of the original 0.9999999, and the ceil methods that use this new value are successful for inputs as small as 0x1p-24 (IEEE machine epsilon)

    opened by jmcwilliams403 0
  • define machine epsilon constants

    define machine epsilon constants

    These are the IEEE-prescribed values already used in this library. I have assigned them names and also implemented the named versions in the Hasher class which already uses them. Their descriptions might need more elaboration regarding their properties.

    opened by jmcwilliams403 0
Releases(v0.1.5)
  • v0.1.5(Nov 30, 2022)

    This release is small, but adds one tiny method to BitConversion so it can be emulated on GWT, adds constants for the vertices and faces of the five Platonic solids in 3D, and adds some more "filling methods" to ArrayTools. The new BitConversion method is int imul(int, int), which acts exactly like int multiplication on non-GWT platforms. But, GWT defaults to behaving strangely when both ints are fairly large -- where on desktop, 0x7fffffff * 0x7fffffff overflows and returns 1, on GWT it vastly exceeds the normal limits for an int and returns 4611686014132420600. The new BitConversion.imul() method calls the JS method Math.imul(), and so it acts like that does and like desktop Java does, overflowing and returning 1 in the aforementioned case. The constants for the Platonic solids, like the icosahedron and cube, are in ShapeTools; there's double and float variants, and versions with an edge length of 1 as well as versions that are inscribed in a unit sphere. The filling methods in ArrayTools allow you to get various placeholder Strings (useful for testing), in arrays or random-accessed. You can access Greek letter names, names of demons from the Ars Goetia, and the names of 118 chemical elements, either all at once or in individual sets. Many of these Strings were used already to seed the 428 Hasher instances in Hasher.predefined, and there are more than twice as many Hashers as there were.

    Source code(tar.gz)
    Source code(zip)
    digital-0.1.5-javadoc.jar(239.10 KB)
    digital-0.1.5-sources.jar(88.17 KB)
    digital-0.1.5.jar(89.55 KB)
  • v0.1.4(Oct 5, 2022)

    This release breaks some backwards compatibility in order to fix compatibility (and avoid parser bugs) in related libraries. Changes are mostly limited to Base:

    • Base86 had its digits change because some were invalid in the String serialization juniper uses.
    • Scrambled bases also can use different digits, because some were invalid sometimes.
    • Writing an unsigned float or double prefixes with . now (they are still read by readDouble()).
    • Writing a signed float or double is often much shorter, doesn't have a prefix, and uses a reversed byte order (it is read the same way).

    There are some added APIs:

    • BitConversion.doubleToReversedLongBits() and BitConversion.reversedLongBitsToDouble() are just like their float-int counterparts, but for double-long conversions.
    • AlternateRandom is here for code that doesn't use juniper but does use ArrayTools.shuffle() a lot. It uses the same algorithm as PasarRandom, which should be in juniper 0.1.5 soon, but doesn't have as many added features.
    • MathTools.boundedInt() and MathTools.boundedLong() allow taking a typically-random number and getting a typically-smaller, bounded random number from it with some guarantee of somewhat random results for random inputs.

    Even though this is a point release, it should probably be 0.2.0 because it changes some behavior. Whoops. The behavior was broken, so a fix was required.

    Source code(tar.gz)
    Source code(zip)
    digital-0.1.4-javadoc.jar(219.75 KB)
    digital-0.1.4-sources.jar(76.65 KB)
    digital-0.1.4.jar(73.20 KB)
  • v0.1.3(Sep 26, 2022)

    This release almost entirely affects TrigTools. The lookup table it uses for sin() and cos() is now more precise, and for the times when a LUT isn't appropriate (such as when cache isn't readily available, or the limited possible return values are a noticeable issue), there are now sinSmooth() and cosSmooth() versions of sin() and cos() that don't use a table. These also have float and double versions, as well as versions that use radians, degrees, and turns. Big thanks to 7th-century astronomer Bhaskara I for the basis for the smooth function, and WimC from Stack Exchange for improving on it.

    Source code(tar.gz)
    Source code(zip)
    digital-0.1.3-javadoc.jar(211.45 KB)
    digital-0.1.3-sources.jar(73.91 KB)
    digital-0.1.3.jar(70.66 KB)
  • v0.1.2(Sep 8, 2022)

    This release only changes the TrigTools.tan() methods and related versions in turns and degrees. The earlier version used an optimization on sin()/cos(), but even small errors in the sine table became grossly magnified in the tan() results. This was starkly noticeable in a demo for juniper's random number distributions, where the Cauchy distribution (which uses tan() in its calculation) went from looking like this with the old tan(), to looking like this with the new tan(). The measured absolute, relative, and maximum error values are all better with the current tan(), though it isn't as fast on Java 8 (still an improvement over Math.tan() in speed). Like the old tan(), the error gets significantly worse as the correct result gets larger, but not as badly as before.

    Source code(tar.gz)
    Source code(zip)
    digital-0.1.2-javadoc.jar(209.41 KB)
    digital-0.1.2-sources.jar(72.34 KB)
    digital-0.1.2.jar(70.09 KB)
  • v0.1.1(Aug 29, 2022)

    This release is very small; it adds a quick way to calculate the nth Fibonacci number and a more-precise way to calculate the nth root of a float. The MathTools.fibonacci() method takes an int or long and returns that type; it uses something close to Binet's algorithm. However, it has been subtly adjusted to counteract floating-point error, so it can return correct results for inputs up to and including 77 instead of 71 (which is what it would manage without adjustment). The MathTools.nthrt() method gets raises the first input to the reciprocal of the second input, just as you would normally do for something that gets the nth root. It then goes beyond that to correct results that are within a tolerance of an integer, returning the close-by int value rather than a potentially-slightly-off float value.

    That's all there is here! Mostly I wanted to keep digital up-to-date as I update related libraries.

    Source code(tar.gz)
    Source code(zip)
    digital-0.1.1-javadoc.jar(208.29 KB)
    digital-0.1.1-sources.jar(70.93 KB)
    digital-0.1.1.jar(69.82 KB)
  • v0.1.0(Aug 11, 2022)

    This release has much more support for shuffling 1D arrays, 2D arrays, and sections of 1D arrays in the ArrayTools class. In MathTools, there's lots more commonly-defined constants for things like the square roots of 2, 3, and 5, almost all contributed by @jmcwilliams403 (thanks!). That's about it! The shuffling should make some code in other libraries a little simpler.

    Source code(tar.gz)
    Source code(zip)
    digital-0.1.0-javadoc.jar(206.94 KB)
    digital-0.1.0-sources.jar(69.92 KB)
    digital-0.1.0.jar(69.63 KB)
  • v0.0.4(Jul 23, 2022)

    This release mostly adds double variants on... well, almost all methods that were previously float-only. It also improves some behavior in ArrayTools (avoiding unexpected copying in some places), and adds ArrayTools.set() and ArrayTools.fill3D, plus generic methods that can alter 2D and 3D Object arrays in some cases. In MathTools, most versions of floor() and ceil() gave incorrect results for negative integers; this has been fixed. The variants on floor() that have a smaller valid domain are now uniformly called fastFloor(); there's also fastCeil().

    So, in general, a nicely-sized set of fixes and additions.

    Source code(tar.gz)
    Source code(zip)
    digital-0.0.4-javadoc.jar(199.45 KB)
    digital-0.0.4-sources.jar(67.02 KB)
    digital-0.0.4.jar(66.09 KB)
  • v0.0.3(Jun 22, 2022)

  • v0.0.2(May 16, 2022)

    This is the first release of digital on GitHub, though 0.0.1 was released to Maven Central. Relative to 0.0.1, this is mostly a bugfix release and provides only a few new features. If you're just looking at this for the first time, though, it has a fair amount of features for its low number. Base should be useful for zero-dependency parsing of numbers in almost-arbitrary base systems, including some Java-8-compatible methods with near-equivalents that only were added to the JDK in later versions. TrigTools has trigonometry methods in radians, degrees, and (my preference) turns, including inverse trigonometric methods like asin() and atan2(); it also has a lookup table for sin()/cos() that is publicly available. MathTools has a grab bag of methods, many from libGDX. It goes on for a bit; anyway, I hope this is useful!

    Source code(tar.gz)
    Source code(zip)
    digital-0.0.2-javadoc.jar(187.91 KB)
    digital-0.0.2-sources.jar(61.18 KB)
    digital-0.0.2.jar(61.11 KB)
Owner
Tommy Ettinger
There was a time when I made games, or tried as much. Now I make game-related libraries, by the truckload. Commit Streak for Life.
Tommy Ettinger
Table-Computing (Simplified as TC) is a distributed light weighted, high performance and low latency stream processing and data analysis framework. Milliseconds latency and 10+ times faster than Flink for complicated use cases.

Table-Computing Welcome to the Table-Computing GitHub. Table-Computing (Simplified as TC) is a distributed light weighted, high performance and low la

Alibaba 34 Oct 14, 2022
Eclipse Collections is a collections framework for Java with optimized data structures and a rich, functional and fluent API.

English | 中文 | Deutsch | Español | Ελληνικά | Français | 日本語 | Norsk (bokmål) | Português-Brasil | Русский | हिंदी Eclipse Collections is a comprehens

Eclipse Foundation 2.1k Dec 29, 2022
A Java library for quickly and efficiently parsing and writing UUIDs

fast-uuid fast-uuid is a Java library for quickly and efficiently parsing and writing UUIDs. It yields the most dramatic performance gains when compar

Jon Chambers 142 Jan 1, 2023
gRPC and protocol buffers for Android, Kotlin, and Java.

Wire “A man got to have a code!” - Omar Little See the project website for documentation and APIs. As our teams and programs grow, the variety and vol

Square 3.9k Jan 5, 2023
The Java collections framework provides a set of interfaces and classes to implement various data structures and algorithms.

Homework #14 Table of Contents General Info Technologies Used Project Status Contact General Information Homework contains topics: Sorting an ArrayLis

Mykhailo 1 Feb 12, 2022
Immutable key/value store with efficient space utilization and fast reads. They are ideal for the use-case of tables built by batch processes and shipped to multiple servers.

Minimal Perfect Hash Tables About Minimal Perfect Hash Tables are an immutable key/value store with efficient space utilization and fast reads. They a

Indeed Engineering 92 Nov 22, 2022
High Performance data structures and utility methods for Java

Agrona Agrona provides a library of data structures and utility methods that are a common need when building high-performance applications in Java. Ma

Real Logic 2.5k Jan 5, 2023
Hollow is a java library and toolset for disseminating in-memory datasets from a single producer to many consumers for high performance read-only access.

Hollow Hollow is a java library and toolset for disseminating in-memory datasets from a single producer to many consumers for high performance read-on

Netflix, Inc. 1.1k Dec 25, 2022
Java Collections till the last breadcrumb of memory and performance

Koloboke A family of projects around collections in Java (so far). The Koloboke Collections API A carefully designed extension of the Java Collections

Roman Leventov 967 Nov 14, 2022
LWJGL is a Java library that enables cross-platform access to popular native APIs useful in the development of graphics (OpenGL, Vulkan), audio (OpenAL), parallel computing (OpenCL, CUDA) and XR (OpenVR, LibOVR) applications.

LWJGL - Lightweight Java Game Library 3 LWJGL (https://www.lwjgl.org) is a Java library that enables cross-platform access to popular native APIs usef

Lightweight Java Game Library 4k Dec 29, 2022
A modern I/O library for Android, Kotlin, and Java.

Okio See the project website for documentation and APIs. Okio is a library that complements java.io and java.nio to make it much easier to access, sto

Square 8.2k Dec 31, 2022
Immutable in-memory R-tree and R*-tree implementations in Java with reactive api

rtree In-memory immutable 2D R-tree implementation in java using RxJava Observables for reactive processing of search results. Status: released to Mav

Dave Moten 999 Dec 20, 2022
RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.

RxJava: Reactive Extensions for the JVM RxJava is a Java VM implementation of Reactive Extensions: a library for composing asynchronous and event-base

ReactiveX 46.7k Dec 30, 2022
Lightweight threads for Java, with message passing, nio, http and scheduling support.

Kilim: Continuations, Fibers, Actors and message passing for the JVM

Sriram Srinivasan 1.7k Jan 3, 2023
Comparison between Java and Common Lisp solutions to a phone-encoding problem described by Prechelt

Prechelt Phone Number Encoding This project implements the phone number encoding described by Lutz Prechelt in his article for the COMMUNICATIONS OF T

Renato Athaydes 27 Nov 30, 2021
Jalgorithm is an open-source Java library which has implemented various algorithms and data structure

We loved Java and algorithms, so We made Jalgorithm ❤ Jalgorithm is an open-source Java library which has implemented various algorithms and data stru

Muhammad Karbalaee 35 Dec 15, 2022
Persistent (immutable) collections for Java and Kotlin

What are Dexx Collections? Dexx Collections are a port of Scala's immutable, persistent collection classes to pure Java. Persistent in the context of

Andrew O'Malley 208 Sep 30, 2022