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.log10(10.0) produces 0.9999999999999999, instead of 1.0, when compiled with -exclude @vlib/math/*.c.v #23136

Closed
spytheman opened this issue Dec 11, 2024 · 4 comments
Labels
Bug This tag is applied to issues which reports bugs. Modules: math Bugs to the math approximations or to the libm wrappers Status: Confirmed This bug has been confirmed to be valid by a contributor.

Comments

@spytheman
Copy link
Member

spytheman commented Dec 11, 2024

V doctor:

V full version: V 0.4.8 aeaf607.de3b184
OS: linux, Ubuntu 20.04.6 LTS
Processor: 4 cpus, 64bit, little endian, Intel(R) Core(TM) i3-3225 CPU @ 3.30GHz

getwd: /space/v/oo
vexe: /home/delian/code/v/v
vexe mtime: 2024-12-07 08:07:40

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

Git version: git version 2.46.0
Git vroot status: weekly.2024.49-35-gd978efc7 (30 commit(s) behind V master)
.git/config present: true

CC version: cc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0
emcc version: N/A
thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9

What did you do?
./v -g -o vdbg cmd/v && ./vdbg -exclude @vlib/math/*.c.v run math_log10_bug.v

import math

assert math.log10(10.0) == 1.0

What did you expect to see?

A working program, without a failing assertion, even with ./v -exclude @vlib/math/*.c.v run math_log10_bug.v

What did you see instead?

math_log10_bug.v:3: FAIL: fn main.main: assert math.log10(10.0) == f64(1.0)
  left value: math.log10(10.0) = 0.9999999999999999
  right value: f64(1.0) = 1.0
V panic: Assertion failed...
v hash: d978efc
/tmp/v_1000/math_log10_bug.01JEVAKEVQ5SGX427N7W3NWQ2W.tmp.c:4148: at _v_panic: Backtrace
/tmp/v_1000/math_log10_bug.01JEVAKEVQ5SGX427N7W3NWQ2W.tmp.c:6839: by main__main
/tmp/v_1000/math_log10_bug.01JEVAKEVQ5SGX427N7W3NWQ2W.tmp.c:6866: by main

Note: without -exclude @vlib/math/*.c.v there is no issue, after #23129 .

With it, the problem still remains, because in this case, the program uses the pure V approximation for the log function.

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.

Huly®: V_0.6-21573

@spytheman spytheman added Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. Modules: math Bugs to the math approximations or to the libm wrappers labels Dec 11, 2024
@spytheman spytheman changed the title math.log10(10.0) produces 0.9999999999999999, instead of 1.0, when compiled with -exclude @vlib/math/*.c.v math.log10(10.0) produces 0.9999999999999999, instead of 1.0, when compiled with -exclude @vlib/math/*.c.v Dec 11, 2024
@spytheman
Copy link
Member Author

Another way to reproduce the problem is by running this:

import math
dump( math.log10(-0.00000000000000001) )
dump( math.log10(0.0) )
dump( math.log10(0.00000000000000001) )
dump( math.log10(1.0) )
dump( math.log10(10.0) )
dump( math.log10(100.0) )
dump( math.log10(1000.0) )
dump( math.log10(10000.0) )
dump( math.log10(1000000.0) )
dump( math.log10(10000000.0) )
dump( math.log10(100000000.0) )

with v -exclude @vlib/math/*.c.v run show.v:

#0 19:04:51 ^ master /v/oo>./v -exclude @vlib/math/*.c.v run show.v
[show.v:2] math.log10(-0.00000000000000001): nan
[show.v:3] math.log10(0.0): -inf
[show.v:4] math.log10(0.00000000000000001): -16.999999999999996
[show.v:5] math.log10(1.0): 0.0
[show.v:6] math.log10(10.0): 0.9999999999999999
[show.v:7] math.log10(100.0): 1.9999999999999998
[show.v:8] math.log10(1000.0): 2.9999999999999996
[show.v:9] math.log10(10000.0): 3.9999999999999996
[show.v:10] math.log10(1000000.0): 5.999999999999999
[show.v:11] math.log10(10000000.0): 6.999999999999999
[show.v:12] math.log10(100000000.0): 7.999999999999999
#0 19:04:57 ^ master /v/oo>

While with the C libm wrappers, which are used by default, the same code produces:

#0 19:04:59 ^ master /v/oo>./v run show.v
[show.v:2] math.log10(-0.00000000000000001): nan
[show.v:3] math.log10(0.0): -inf
[show.v:4] math.log10(0.00000000000000001): -17.0
[show.v:5] math.log10(1.0): 0.0
[show.v:6] math.log10(10.0): 1.0
[show.v:7] math.log10(100.0): 2.0
[show.v:8] math.log10(1000.0): 3.0
[show.v:9] math.log10(10000.0): 4.0
[show.v:10] math.log10(1000000.0): 6.0
[show.v:11] math.log10(10000000.0): 7.0
[show.v:12] math.log10(100000000.0): 8.0
#0 19:05:36 ^ master /v/oo>

@jorgeluismireles
Copy link

Let alone logs, seems the print rounding affects somehow:

const tens := {
	'∞ ':  f64(10)
	'17': 9.99999999999999999
	'16': 9.9999999999999999
	'15': 9.999999999999999
	'14': 9.99999999999999
}

fn main() {
	for decimals, ten in tens {
		println('${decimals} ${ten}')
	}
}

Result:

∞  10.0
17 10.0
16 10.0
15 9.999999999999998
14 9.99999999999999

I mean, the print alone of fifteen decimals figure above modified last 9 into 8.

@penguindark
Copy link
Member

The print does not affect the assert: assert math.log10(10.0) == 1.0 that must stay true!
About the string conversion of f32/f64, when you convert a float 32 iee754 32bit to string you must expect a precision from 6 to 9 digits.
Using f64 you may expect max 15 digit but a correct representation can be impossible in a lot of cases.
https://en.wikipedia.org/wiki/IEEE_754
We have used in V the best available algos at the time to convert for float 32/64 bit to string.
V code f64 to str

@jorgeluismireles
Copy link

assert math.log10(10.0) == 1.0 that must stay true!

Yes, this is what mister Golang said:

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Log10(10.0))
	fmt.Println(9.999999999999999)
}

Result

1
9.999999999999998

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 Bugs to the math approximations or to the libm wrappers Status: Confirmed This bug has been confirmed to be valid by a contributor.
Projects
None yet
Development

No branches or pull requests

3 participants