mojo icon indicating copy to clipboard operation
mojo copied to clipboard

[BUG]: Default argument value bugs

Open mojodojodev opened this issue 1 year ago • 0 comments

Partial use of default values

@value
struct Benchmark:
    fn __init__(
        inout self,
        num_warmup: Int = 2,
        max_iters: Int = 100_000,
        min_time_ns: Int = 500_000_000,
        max_time_ns: Int = 1000_000_000,
    ):
        print(num_warmup)
        print(max_iters)
        print(min_time_ns)
        print(max_time_ns)

let b = Benchmark(5, 10)
5
10
2
100000

It pushes the first two default values that weren't used to position 3 and 4.

You can see this does effect the current implementation of Benchmark in the standard library:

from Time import sleep

fn bench_args():
    fn sleeper():
        print("sleeping 30,000ns")
        sleep(3e-5)
    
    let b = Benchmark(0, 10)
    print("max benchmark milliseconds:", b.max_time_ns)

    _ = b.run[sleeper]()

bench_args()
max benchmark milliseconds: 100000
sleeping 30,000ns
sleeping 30,000ns
sleeping 30,000ns

The max is 100,000ns, not 1,000,000,000ns as intended, so only three sets of 30,000ns can run of the intended 10 max iterations.

Max Digits

In the Mojo playground, when two or more arguments are used with default values for Int, the max digits is 11, anything more will remove digits at runtime. This problem doesn't occur for F64.

This works fine:

fn default_val(a: Int = 12345678901, b: Int = 12345678901):
    print(a, b)

default_val()
12345678901 12345678901

Here a has 12 digits and so will only print 10 digits:

fn default_val_2(a: Int = 123456789012, b: Int = 12345678901):
    print(a, b)

default_val_2()
1234567890 12345678901

Both arguments here have 13 digits, and so will only print 9 digits

fn default_val_3(a: Int = 1234567890123, b: Int = 1234567890123):
    print(a, b)

default_val_3()
123456789 123456789

System information

11:hugetlb:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podddf90e49_6441_40c3_988f_9c67757e9546.slice/cri-containerd-2e45bd416836a026b676986c3ea9aa9790058d18549073fd5e3e726eb3bc0ae7.scope
10:memory:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podddf90e49_6441_40c3_988f_9c67757e9546.slice/cri-containerd-2e45bd416836a026b676986c3ea9aa9790058d18549073fd5e3e726eb3bc0ae7.scope
9:cpu,cpuacct:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podddf90e49_6441_40c3_988f_9c67757e9546.slice/cri-containerd-2e45bd416836a026b676986c3ea9aa9790058d18549073fd5e3e726eb3bc0ae7.scope
8:perf_event:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podddf90e49_6441_40c3_988f_9c67757e9546.slice/cri-containerd-2e45bd416836a026b676986c3ea9aa9790058d18549073fd5e3e726eb3bc0ae7.scope
7:devices:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podddf90e49_6441_40c3_988f_9c67757e9546.slice/cri-containerd-2e45bd416836a026b676986c3ea9aa9790058d18549073fd5e3e726eb3bc0ae7.scope
6:freezer:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podddf90e49_6441_40c3_988f_9c67757e9546.slice/cri-containerd-2e45bd416836a026b676986c3ea9aa9790058d18549073fd5e3e726eb3bc0ae7.scope
5:blkio:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podddf90e49_6441_40c3_988f_9c67757e9546.slice/cri-containerd-2e45bd416836a026b676986c3ea9aa9790058d18549073fd5e3e726eb3bc0ae7.scope
4:net_cls,net_prio:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podddf90e49_6441_40c3_988f_9c67757e9546.slice/cri-containerd-2e45bd416836a026b676986c3ea9aa9790058d18549073fd5e3e726eb3bc0ae7.scope
3:cpuset:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podddf90e49_6441_40c3_988f_9c67757e9546.slice/cri-containerd-2e45bd416836a026b676986c3ea9aa9790058d18549073fd5e3e726eb3bc0ae7.scope
2:pids:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podddf90e49_6441_40c3_988f_9c67757e9546.slice/cri-containerd-2e45bd416836a026b676986c3ea9aa9790058d18549073fd5e3e726eb3bc0ae7.scope
1:name=systemd:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podddf90e49_6441_40c3_988f_9c67757e9546.slice/cri-containerd-2e45bd416836a026b676986c3ea9aa9790058d18549073fd5e3e726eb3bc0ae7.scope
0::/

mojodojodev avatar May 29 '23 06:05 mojodojodev