Skip to content

Commit 80cee3d

Browse files
authored
Merge pull request #1 from Synt4xErr0r4/decimal
Added decimal support
2 parents 19d8d54 + affe9b4 commit 80cee3d

26 files changed

+2430
-624
lines changed

README.md

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# ieee754-java [![javadoc](https://img.shields.io/endpoint?label=javadoc&url=https://javadoc.syntaxerror.at/ieee754-java/%3Fbadge=true%26version=latest)](https://javadoc.syntaxerror.at/ieee754-java/latest) ![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/Synt4xErr0r4/ieee754-java/maven.yml)
22

3-
A Java 19 library for converting between IEEE 754 binary floating-point numbers and Java's BigDecimal
3+
A Java 19 library for converting between IEEE 754 binary and decimal floating-point numbers and Java's BigDecimal
44

55
## Overview
66

7-
This library can convert `java.math.BigDecimal`s into IEEE 754 binary representations. By default, binary16, binary32, binary64, binary128, binary256, and x87's extended precision (binary80) are supported. There are also 3 larger types (binary512, binary1024, binary2048) available, these are, however, most likely impractial for any purpose due to their *extremely* long encoding/decoding times.
7+
This library can convert `java.math.BigDecimal`s into IEEE 754 binary representations. By default, binary16, binary32, binary64, binary128, binary256, x87's extended precision (binary80), decimal32, decimal64 and decimal128 are supported. There are also 3 larger types (binary512, binary1024, binary2048) available, these are, however, most likely impractial for any purpose due to their *extremely* long encoding/decoding times.
88

99
## Getting started
1010

11-
In order to use the code, you can either [download the jar](https://github.com/Synt4xErr0r4/ieee754-java/releases/download/1.0.0/ieee754-java-1.0.0.jar), or use the Maven dependency:
11+
In order to use the code, you can either [download the jar](https://github.com/Synt4xErr0r4/ieee754-java/releases/download/2.0.0/ieee754-java-2.0.0.jar), or use the Maven dependency:
1212

1313
```xml
1414
<!-- Repository -->
@@ -23,7 +23,7 @@ In order to use the code, you can either [download the jar](https://github.com/S
2323
<dependency>
2424
<groupId>at.syntaxerror</groupId>
2525
<artifactId>ieee754-java</artifactId>
26-
<version>1.0.0</version>
26+
<version>2.0.0</version>
2727
</dependency>
2828
```
2929

@@ -44,13 +44,16 @@ There are several predefined types:
4444
- `Binary512`*
4545
- `Binary1024`*
4646
- `Binary2048`*
47+
- `Decimal32`
48+
- `Decimal64`
49+
- `Decimal128`
4750

4851
\* These types are for demonstration purposes only, their parameters do not follow any official IEEE 754 standard, encoding/decoding might take *very* long and result might not be accurate. Use with caution.
4952

5053
For any of the predefined types, there is a static field called `FACTORY`, which is used to create classes of their respective type:
5154

5255
- `create(int signum, BinaryType type)`
53-
- `create(int signum, BigDecimal value)`
56+
- `create(int signum, BigDecimal value)` (only useful for signed zeros)
5457
- `create(BigDecimal value)`
5558
- `create(Number value)`
5659

@@ -74,7 +77,7 @@ import java.math.BigInteger;
7477
BigInteger bin = value.encode();
7578
```
7679

77-
The binary representation can also be decoded. The `BinaryCodec` class, accessible via `Binary32`'s `CODEC` field, provides the method `decode`:
80+
The binary representation can also be decoded. The `FloatingCodec` class, accessible via `Binary32`'s `CODEC` field, provides the method `decode`:
7881

7982
```java
8083
Binary32 decoded = Binary32.CODEC.decode(bin);
@@ -86,13 +89,44 @@ The value can then be retrieved for further computations as a `BigDecimal` via t
8689
BigDecimal bigdec = decoded.getBigDecimal();
8790
```
8891

89-
This is applicable to all predefined types; Also, any class inheriting from `Binary<T>` has access to various helper methods, which are listed in the [JavaDoc](https://javadoc.syntaxerror.at/ieee754-java/latest/ieee754java/at/syntaxerror/ieee754/binary/Binary.html).
92+
This is applicable to all predefined types; Also, any class inheriting from `Floating<T>` (or its subclasses `Binary<T>` and `Decimal<T>`) has access to various helper methods, which are listed in the [JavaDoc](https://javadoc.syntaxerror.at/ieee754-java/latest/ieee754java/at/syntaxerror/ieee754/Floating.html).
93+
94+
### Decimal Encoding
95+
96+
There are two ways IEEE 754 decimal floating-point numbers can be encoded:
97+
98+
- BID (binary integer decimal) format
99+
- DPD (densly packed decimal) format
100+
101+
Both formats encode the same range of numbers.
102+
103+
The `DecimalCodec<T>` class also provides separate encoding/decoding methods for the two modes:
104+
105+
- `encodeBID(T value)`
106+
- `encodeDPD(T value)`
107+
- `decodeBID(BigInteger value)`
108+
- `decodeDPD(BigInteger value)`
109+
110+
Similar methods are available in `Decimal<T>`:
111+
112+
- `encodeBID()`
113+
- `encodeDPD()`
114+
115+
Methods that do not have a suffix (`BID` or `DPD`) call one of these methods depending on the default mode.
116+
By default, `BID` is used.
117+
This can be changed by settings the `DEFAULT_CODING` field in the `Decimal` class
118+
to either `DecimalCoding.BINARY_INTEGER_DECIMAL` or `DecimalCoding.DENSLY_PACKED_DECIMAL`.
90119

91120
### Custom Types
92121

93122
You can also create custom floating-point types:
94123

95-
When extending `Binary<T>`, you need to implement the method `getCodec`, which returns a `BinaryCodec<T>`. This codec can be created by using its constructor, which takes the number of exponent bits, mantissa bits, whether there is an implicit bit, and a `BinaryFactory<T>`. The factory is an interface where you need to implement constructors for your new class.
124+
When extending `Floating<T>` (or the subclass `Binary<T>` or `Decimal<T>`), you need to implement the method `getCodec`, which returns a `FloatingCodec<T>`. The `FloatingCodec<T>`, however, is an abstract class. If you want to implement a `Binary`-like codec, use the `BinaryCodec<T>` class instead. For `Decimal`-like codecs, `DecimalCodec<T>` exists. These codecs can be created by using their constructors.
125+
126+
The `BinaryCodec<T>` constructor takes the number of exponent bits, significand bits, whether there is an implicit bit and a `FloatingFactory<T>`.
127+
The `DecimalCodec<T>` constructor takes the number of combination field bits, significand bits and a `FloatingFactory<T>`
128+
129+
The factory is an interface where you need to implement constructors for your new class.
96130

97131
Take a look at the various predefined types to see how they are implemented.
98132

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>at.syntaxerror</groupId>
44
<artifactId>ieee754-java</artifactId>
5-
<version>1.0.0</version>
5+
<version>2.0.0</version>
66
<name>IEEE754-Java</name>
77
<description>A Java 19 library for converting between IEEE 754 binary and decimal and BigDecimal</description>
88
<licenses>

0 commit comments

Comments
 (0)