Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Precise Values

[![](https://jitpack.io/v/Krumuvecis/PreciseValues.svg)](https://jitpack.io/#Krumuvecis/PreciseValues)

by [Krumuvecis](https://github.com/Krumuvecis)

Free to use and modify for whatever purposes. No copyrights apply.


## Abstract

This is mainly a library for a new data type `PreciseNumber`, but also has other functionality.


### PreciseNumber

A new data type representing value +/- error [like this](https://www2.southeastern.edu/Academics/Faculty/rallain/plab194/error.html).

`PreciseNumber` consists of 2 parts:
* `BigDecimal` value part;
* `NumberError` error part, which further consists of 2 parts:
* `BigDecimal` value of the error;
* `ErrorType` type of the error (enum).


### Other functionality

A list of functions:
* *something*
* *somethingsomething*
* *somethingsomethingmore*
* ...


## Requirements

* JDK: 18
* Maven compiler: 18
* Maven Surefire: 3.0.0-M7
* jetbrains annotations: 23.0.0


## Notes

*Under construction, expect changes...*
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.Krumuvecis</groupId>
<groupId>com.github.KruMF</groupId>
<artifactId>PreciseValues</artifactId>
<version>0.2</version>
<version>0.3</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
27 changes: 18 additions & 9 deletions src/main/java/preciseValues/errorModel/NumberError.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* TODO: finish this javadoc
*/
public class NumberError {
public static final NumberError NON_NULL_ERROR = new NumberError();
private static final BigDecimal
UNDEFINED_ERROR_VALUE = new BigDecimal(0),
NON_NULL_AVERAGE_VALUE = new BigDecimal(0);
Expand Down Expand Up @@ -65,45 +66,53 @@ public NumberError(@Nullable ErrorType errorType, @Nullable BigDecimal errorValu
* @param errorType Type of error to return.
* @param averageValue Average value for reference.
*
* @return Value of the error.
* @return Error of the desired type.
*/
public final @NotNull BigDecimal getError(
public final @NotNull NumberError getError(
@Nullable ErrorType errorType,
@Nullable BigDecimal averageValue) {
return switch (Objects.requireNonNullElse(errorType, ErrorType.UNDEFINED)) {
case ABSOLUTE -> getAbsoluteError(averageValue);
case RELATIVE -> getRelativeError(averageValue);
default -> UNDEFINED_ERROR_VALUE;
default -> NON_NULL_ERROR;
};
}

private @NotNull BigDecimal getAbsoluteError(@Nullable BigDecimal averageValue) {
private @NotNull NumberError getAbsoluteError(@Nullable BigDecimal averageValue) {
return new NumberError(ErrorType.ABSOLUTE, getAbsoluteErrorValue(averageValue));
}

private @NotNull BigDecimal getAbsoluteErrorValue(@Nullable BigDecimal averageValue) {
return switch (errorType) {
case ABSOLUTE -> errorValue;
case RELATIVE -> getAbsoluteFromRelative(
case RELATIVE -> getAbsoluteValueFromRelativeValue(
errorValue,
Objects.requireNonNullElse(averageValue, NON_NULL_AVERAGE_VALUE));
default -> UNDEFINED_ERROR_VALUE;
};
}

private static @NotNull BigDecimal getAbsoluteFromRelative(
private static @NotNull BigDecimal getAbsoluteValueFromRelativeValue(
@NotNull BigDecimal errorValue,
@NotNull BigDecimal averageValue) {
return averageValue.multiply(errorValue);
}

private @NotNull BigDecimal getRelativeError(@Nullable BigDecimal averageValue) {
private @NotNull NumberError getRelativeError(@Nullable BigDecimal averageValue) {
return new NumberError(ErrorType.RELATIVE, getRelativeErrorValue(averageValue));
}

private @NotNull BigDecimal getRelativeErrorValue(@Nullable BigDecimal averageValue) {
return switch (errorType) {
case ABSOLUTE -> getRelativeFromAbsolute(
case ABSOLUTE -> getRelativeValueFromAbsoluteValue(
errorValue,
Objects.requireNonNullElse(averageValue, NON_NULL_AVERAGE_VALUE));
case RELATIVE -> errorValue;
default -> UNDEFINED_ERROR_VALUE;
};
}

private static @NotNull BigDecimal getRelativeFromAbsolute(
private static @NotNull BigDecimal getRelativeValueFromAbsoluteValue(
@NotNull BigDecimal errorValue,
@NotNull BigDecimal averageValue) {
BigDecimal result;
Expand Down
114 changes: 114 additions & 0 deletions src/main/java/preciseValues/lists/AbstractPList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package preciseValues.lists;

import preciseValues.errorModel.ErrorType;
import preciseValues.errorModel.NumberError;
import preciseValues.pNumber.PNumber;

import java.util.List;
import java.util.ArrayList;
import java.util.Objects;
import java.math.BigDecimal;
import java.math.RoundingMode;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

//TODO: add javadoc
public abstract class AbstractPList implements PListInterface {

public List<PListMember> startingList, resultList;
public boolean calculated;

private BigDecimal average;
private BigDecimal standardDeviation;

//TODO: add javadoc
AbstractPList(@Nullable List<PListMember> members) {
calculated = false;
startingList = List.copyOf(Objects.requireNonNullElse(members, new ArrayList<>()));
}

//TODO: finish this and add javadoc
@Override
public void calculate(BigDecimal probability) {
resultList = List.copyOf(startingList);
setAverage();
setOffsets();
setStandardDeviation();
//TODO: get discrepancies
//TODO: repeat
calculated = true;
}

//TODO: add javadoc
public final int getStartingMemberCount() {
return startingList.size();
}

//TODO: add javadoc
@Override
public final int getMemberCount() {
return resultList.size();
}

//TODO: add javadoc
public void setAverage() {
this.average = getMemberSum().divide(
new BigDecimal(getMemberCount()),
RoundingMode.HALF_UP);
}

private @NotNull BigDecimal getMemberSum() {
BigDecimal sum = BigDecimal.ZERO;
for (PListMember member : resultList) {
sum = sum.add(member.getValue().getValue());
}
return sum;
}

private void setOffsets() {
BigDecimal average = getAverage();
for (PListMember member : resultList) {
member.setOffsets(average);
}
}

private void setStandardDeviation() {
//TODO: finish this
this.standardDeviation = null;
}

//TODO: add javadoc
@SuppressWarnings("unused")
public final @NotNull BigDecimal getStandardDeviation() {
return standardDeviation;
}

//TODO: add javadoc
@Override
public final @NotNull BigDecimal getAverage() {
return average;
}

//TODO: add javadoc
@Override
public final @NotNull NumberError getError(@Nullable ErrorType errorType) {
return switch(Objects.requireNonNullElse(errorType, ErrorType.UNDEFINED)) {
case ABSOLUTE -> getAbsoluteError();
case RELATIVE -> getRelativeError();
default -> NumberError.NON_NULL_ERROR;
};
}

//TODO: add javadoc
public abstract @NotNull NumberError getAbsoluteError();

//TODO: add javadoc
public abstract @NotNull NumberError getRelativeError();

//TODO: add javadoc
@Override
public final @NotNull PNumber getAverage(@Nullable ErrorType errorType) {
return new PNumber(getAverage(), getError(errorType));
}
}
52 changes: 0 additions & 52 deletions src/main/java/preciseValues/lists/AbstractPrecisionList.java

This file was deleted.

This file was deleted.

This file was deleted.

Loading