Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

math.big addition yields incorrect results #22310

Closed
squidink7 opened this issue Sep 25, 2024 · 2 comments · Fixed by #22330
Closed

math.big addition yields incorrect results #22310

squidink7 opened this issue Sep 25, 2024 · 2 comments · Fixed by #22330
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Modules: math.big Bugs related to the `math.big` module. Status: Confirmed This bug has been confirmed to be valid by a contributor.

Comments

@squidink7
Copy link
Contributor

squidink7 commented Sep 25, 2024

Describe the bug

When trying to do basic arithmetic using big integers, it often returns results with the signs inverted or numbers being added when they should have been subtracted

Reproduction Steps

import math.big
x := big.integer_from_int(-1)
x + big.integer_from_int(200)

output is -199

Expected Behavior

the above code to return 199

Current Behavior

it returns -199

Possible Solution

No response

Additional Information/Context

No response

V version

0.4.7 be1bb60.9a0f4d8

Environment details (OS name and version, etc.)

V full version: V 0.4.7 be1bb60.9a0f4d8
OS: linux, "Arch Linux"
Processor: 12 cpus, 64bit, little endian, AMD Ryzen 5 7640U w/ Radeon 760M Graphics

getwd: /home/sirsegv
vexe: /home/sirsegv/.v/v
vexe mtime: 2024-09-19 13:10:00

vroot: OK, value: /home/sirsegv/.v
VMODULES: OK, value: /home/sirsegv/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.46.1
Git vroot status: weekly.2024.07-1143-g9a0f4d84 (34 commit(s) behind V master)
.git/config present: true

CC version: cc (GCC) 14.2.1 20240910
thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@squidink7 squidink7 added the Bug This tag is applied to issues which reports bugs. label Sep 25, 2024
@spytheman spytheman added Modules: math.big Bugs related to the `math.big` module. Status: Confirmed This bug has been confirmed to be valid by a contributor. labels Sep 27, 2024
@spytheman
Copy link
Member

Other weird results, that are probably due to the same bug:

import math.big

one := big.integer_from_int(1)
n_one := big.integer_from_int(-1)
x := big.integer_from_int(-1)
y := big.integer_from_int(200)
dump(x)
dump(y)
eprintln("-".repeat(30))
dump(x + y)
eprintln("-".repeat(30))
dump(x + one)
dump(one + x)
eprintln("-".repeat(30))
dump(y + one)
dump(one + y)
eprintln("-".repeat(30))
dump(x + n_one)
dump(n_one + x)
eprintln("-".repeat(30))
dump(y + n_one)
dump(n_one + y)
eprintln("-".repeat(30))
dump(x + y + one)
dump(x + one + y)
dump(one + y + x)
dump(one + x + y)
dump(y + one + x)
dump(y + x + one)
eprintln("-".repeat(30))
dump(x + y + n_one)
dump(x + n_one + y)
dump(n_one + y + x)
dump(n_one + x + y)
dump(y + n_one + x)
dump(y + x + n_one)

producing:

[ii.v:7] x: -1
[ii.v:8] y: 200
------------------------------
[ii.v:10] x + y: -199
------------------------------
[ii.v:12] x + one: 0
[ii.v:13] one + x: 0
------------------------------
[ii.v:15] y + one: 201
[ii.v:16] one + y: 201
------------------------------
[ii.v:18] x + n_one: -2
[ii.v:19] n_one + x: -2
------------------------------
[ii.v:21] y + n_one: 199
[ii.v:22] n_one + y: -199
------------------------------
[ii.v:24] x + y + one: -198
[ii.v:25] x + one + y: 200
[ii.v:26] one + y + x: 200
[ii.v:27] one + x + y: 200
[ii.v:28] y + one + x: 200
[ii.v:29] y + x + one: 200
------------------------------
[ii.v:31] x + y + n_one: -200
[ii.v:32] x + n_one + y: -198
[ii.v:33] n_one + y + x: -200
[ii.v:34] n_one + x + y: -198
[ii.v:35] y + n_one + x: 198
[ii.v:36] y + x + n_one: 198

@spytheman
Copy link
Member

At first glance, perhaps the sign of the result, is determined by just looking at the sign of the receiver 🤔 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Modules: math.big Bugs related to the `math.big` module. Status: Confirmed This bug has been confirmed to be valid by a contributor.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants