Skip to content

Commit

Permalink
Merge branch 'main' into task/modInverse
Browse files Browse the repository at this point in the history
  • Loading branch information
ionspin committed Jul 14, 2024
2 parents 76fedc4 + 2ebf683 commit b367ea8
Show file tree
Hide file tree
Showing 16 changed files with 445 additions and 3,243 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/github-pull-request-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

name: Build project and run tests.
run-name: ${{ github.actor }} pull request
on:
pull_request:
branches:
- main

env:
CHROME_BIN: "chrome"
jobs:
Build-And-Test-Linux:
runs-on: ubuntu-latest
steps:
- name: Setup java
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
- name: Checkout
uses: actions/checkout@v4
- uses: browser-actions/setup-chrome@v1
- run: ./linuxBuild.sh
Build-And-Test-Mac:
runs-on: macos-latest
steps:
- name: Setup java
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
- name: Checkout
uses: actions/checkout@v4
- uses: browser-actions/setup-chrome@v1
- run: ./macBuild.sh
Build-And-Test-Windows:
runs-on: windows-latest
steps:
- name: Setup java
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
- name: Checkout
uses: actions/checkout@v4
- uses: msys2/setup-msys2@v2
- uses: browser-actions/setup-chrome@v1
- shell: msys2 {0}
run: |
./windowsBuild.sh
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ build/
/buildSrc/out
/bignum/node_modules
zignum/
.kotlin/
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

##### 0.3.10-SNAPSHOT - current snapshot
- Fix for #280, return byte array containing zero instead of empty array when big integer is zero.
- Fix for #291, override hashCode as well as equals in ModularBigInteger
- Fix for #292, negate for Modular big intiger did nothing.
- Bump to kotlin 2.0.0
- Add github action for pull request build and test.


##### 0.3.9 - 28.1.2024
- Bump to kotlin 1.9.21
Expand Down
9 changes: 0 additions & 9 deletions bignum/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ kotlin {
if (hostOs == HostOs.LINUX) {
linuxX64("linux")
if (ideaActive.not()) {
linuxArm32Hfp()
linuxArm64()
androidNativeX64()
androidNativeX86()
Expand Down Expand Up @@ -225,14 +224,6 @@ kotlin {

if (ideaActive.not()) {

val linuxArm32HfpMain by getting {
dependsOn(nativeMain)
}

val linuxArm32HfpTest by getting {
dependsOn(nativeTest)
}

val linuxArm64Main by getting {
dependsOn(nativeMain)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ class BigDecimal private constructor(
exponent: Long,
decimalMode: DecimalMode
): BigDecimal {
if (significand == BigInteger.ZERO) {
if (significand.isZero()) {
return BigDecimal(BigInteger.ZERO, exponent, decimalMode)
}
val significandDigits = significand.numberOfDecimalDigits()
Expand All @@ -395,7 +395,7 @@ class BigDecimal private constructor(
decimalMode.decimalPrecision
}
return when {
desiredPrecision > significandDigits -> {
desiredPrecision > significandDigits && !decimalMode.usingScale -> {
val extendedSignificand = significand * BigInteger.TEN.pow(desiredPrecision - significandDigits)
BigDecimal(extendedSignificand, exponent, decimalMode)
}
Expand Down Expand Up @@ -1096,10 +1096,10 @@ class BigDecimal private constructor(
*/
fun add(other: BigDecimal, decimalMode: DecimalMode? = null): BigDecimal {
val resolvedDecimalMode = resolveDecimalMode(this.decimalMode, other.decimalMode, decimalMode)
if (this == ZERO) {
if (this.isZero()) {
return roundOrDont(other.significand, other.exponent, resolvedDecimalMode)
}
if (other == ZERO) {
if (other.isZero()) {
return roundOrDont(this.significand, this.exponent, resolvedDecimalMode)
}
val (first, second, _) = bringSignificandToSameExponent(this, other)
Expand Down Expand Up @@ -1153,10 +1153,10 @@ class BigDecimal private constructor(
fun subtract(other: BigDecimal, decimalMode: DecimalMode? = null): BigDecimal {
val resolvedDecimalMode = resolveDecimalMode(this.decimalMode, other.decimalMode, decimalMode)

if (this == ZERO) {
if (this.isZero()) {
return roundOrDont(other.significand.negate(), other.exponent, resolvedDecimalMode)
}
if (other == ZERO) {
if (other.isZero()) {
return roundOrDont(this.significand, this.exponent, resolvedDecimalMode)
}

Expand Down Expand Up @@ -1514,7 +1514,7 @@ class BigDecimal private constructor(
* the same number i.e. 1.234
*/
private fun removeTrailingZeroes(bigDecimal: BigDecimal): BigDecimal {
if (bigDecimal == ZERO) return this
if (bigDecimal.isZero()) return this
var significand = bigDecimal.significand
var divisionResult = BigInteger.QuotientAndRemainder(bigDecimal.significand, BigInteger.ZERO)
do {
Expand Down Expand Up @@ -1657,7 +1657,7 @@ class BigDecimal private constructor(
* as division.
*/
override fun pow(exponent: Long): BigDecimal {
if (this == ZERO && exponent < 0) {
if (this.isZero() && exponent < 0) {
throw ArithmeticException("Negative exponentiation of zero is not defined.")
}
var result = this
Expand Down Expand Up @@ -2167,7 +2167,7 @@ class BigDecimal private constructor(
}

override fun hashCode(): Int {
if (this == ZERO) {
if (this.isZero()) {
return 0
}
return removeTrailingZeroes(this).significand.hashCode() + exponent.hashCode()
Expand Down Expand Up @@ -2253,7 +2253,7 @@ class BigDecimal private constructor(
* i.e. 123000000 for 1.23E+9 or 0.00000000123 for 1.23E-9
*/
fun toStringExpanded(): String {
if (this == ZERO) {
if (this.isZero()) {
return "0"
}
val digits = significand.numberOfDecimalDigits()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.ionspin.kotlin.bignum.CommonBigNumberOperations
import com.ionspin.kotlin.bignum.NarrowingOperations
import com.ionspin.kotlin.bignum.decimal.BigDecimal
import com.ionspin.kotlin.bignum.integer.base63.array.BigInteger63Arithmetic
import com.ionspin.kotlin.bignum.integer.base63.array.BigInteger63Arithmetic.compareTo
import com.ionspin.kotlin.bignum.modular.ModularBigInteger
import kotlin.math.ceil
import kotlin.math.floor
Expand Down Expand Up @@ -272,14 +273,14 @@ class BigInteger internal constructor(wordArray: WordArray, requestedSign: Sign)
}

override fun subtract(other: BigInteger): BigInteger {
val comparison = arithmetic.compare(this.magnitude, other.magnitude)
if (this == ZERO) {
if (this.isZero()) {
return other.negate()
}
if (other == ZERO) {
if (other.isZero()) {
return this
}
return if (other.sign == this.sign) {
val comparison = arithmetic.compare(this.magnitude, other.magnitude)
when {
comparison > 0 -> {
BigInteger(arithmetic.subtract(this.magnitude, other.magnitude), sign)
Expand Down Expand Up @@ -506,7 +507,7 @@ class BigInteger internal constructor(wordArray: WordArray, requestedSign: Sign)
}

fun pow(exponent: BigInteger): BigInteger {
if (exponent < ZERO)
if (exponent.isNegative)
throw ArithmeticException("Negative exponent not supported with BigInteger")

if (exponent <= Long.MAX_VALUE) {
Expand All @@ -518,9 +519,9 @@ class BigInteger internal constructor(wordArray: WordArray, requestedSign: Sign)

private tailrec fun exponentiationBySquaring(y: BigInteger, x: BigInteger, n: BigInteger): BigInteger {
return when {
n == ZERO -> y
n.isZero() -> y
n == ONE -> x * y
n.mod(TWO) == ZERO -> exponentiationBySquaring(y, x * x, n / 2)
n.mod(TWO).isZero() -> exponentiationBySquaring(y, x * x, n / 2)
else -> exponentiationBySquaring(x * y, x * x, (n - 1) / 2)
}
}
Expand All @@ -529,9 +530,9 @@ class BigInteger internal constructor(wordArray: WordArray, requestedSign: Sign)
if (exponent < 0) {
throw ArithmeticException("Negative exponent not supported with BigInteger")
}
return when (this) {
ZERO -> ZERO
ONE -> ONE
return when {
isZero() -> ZERO
this == ONE -> ONE
else -> {
val sign = if (sign == Sign.NEGATIVE) {
if (exponent % 2 == 0L) {
Expand Down Expand Up @@ -573,8 +574,14 @@ class BigInteger internal constructor(wordArray: WordArray, requestedSign: Sign)
if (isZero()) {
return 1
}
val bitLength = arithmetic.bitLength(magnitude)
val minDigit = ceil((bitLength - 1) * LOG_10_OF_2)
// Search through firsts powersOf10
val powersOf10 = BigInteger63Arithmetic.powersOf10
val quickSearch = powersOf10.indexOfFirst { it > magnitude }
if (quickSearch != -1) {
return quickSearch.toLong()
}
// val bitLength = arithmetic.bitLength(magnitude)
// val minDigit = ceil((bitLength - 1) * LOG_10_OF_2)
// val maxDigit = floor(bitLenght * LOG_10_OF_2) + 1
// val correct = this / 10.toBigInteger().pow(maxDigit.toInt())
// return when {
Expand All @@ -583,13 +590,13 @@ class BigInteger internal constructor(wordArray: WordArray, requestedSign: Sign)
// else -> -1
// }

var tmp = this / 10.toBigInteger().pow(minDigit.toInt())
var tmp = this / TEN.pow(powersOf10.size)
var counter = 0L
while (tmp.compareTo(0) != 0) {
while (!tmp.isZero()) {
tmp /= 10
counter++
}
return counter + minDigit.toInt()
return counter + powersOf10.size
}

override infix fun shl(places: Int): BigInteger {
Expand Down Expand Up @@ -770,7 +777,7 @@ class BigInteger internal constructor(wordArray: WordArray, requestedSign: Sign)

// TODO eh
operator fun times(char: Char): String {
if (this < 0) {
if (this.isNegative) {
throw RuntimeException("Char cannot be multiplied with negative number")
}
var counter = this
Expand Down
3 changes: 0 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@ buildscript {

repositories {
mavenCentral()
gradlePluginPortal()
}

dependencies {
// classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.kotlin}")
classpath("org.jetbrains.dokka:dokka-gradle-plugin:${Versions.dokkaPlugin}")
}
}

allprojects {

repositories {
mavenCentral()
maven {
Expand Down
6 changes: 6 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ plugins {
`kotlin-dsl`
}

buildscript {
repositories {
mavenCentral()
}
}

repositories {
mavenCentral()
}
6 changes: 3 additions & 3 deletions buildSrc/src/main/kotlin/Deps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/

object Versions {
val kotlinCoroutines = "1.8.0-RC2"
val kotlin = "1.9.21"
val kotlinSerialization = "1.6.2"
val kotlinCoroutines = "1.9.0-RC"
val kotlin = "2.0.0"
val kotlinSerialization = "1.7.1"
val dokkaPlugin = "1.9.10"
}

Expand Down
Loading

0 comments on commit b367ea8

Please sign in to comment.