Skip to content

exp2_64() broken for negative inputs #9991

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

Closed
1 task done
LewisGaul opened this issue Oct 21, 2021 · 1 comment · Fixed by #9999
Closed
1 task done

exp2_64() broken for negative inputs #9991

LewisGaul opened this issue Oct 21, 2021 · 1 comment · Fixed by #9999
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@LewisGaul
Copy link
Contributor

Remember to search before filing a new report

  • I searched for this bug and did not find it in the issue tracker, and furthermore, the title I used above will make this new bug report turn up in the search results for my query.

Zig Version

0.9.0-dev.1417+bea447a63

Steps to Reproduce

const std = @import("std");
const print = std.debug.print;
const exp2 = std.math.exp2;

test {
    const val1: f64 = -8;
    print("in: {}, out: {}\n", .{val1, exp2(val1)});

    const val2: f64 = -0x1.a05cc754481d1p-2;
    print("in: {}, out: {}\n", .{val2, exp2(val2)});
}

Expected Behavior

Should successfully compute 2^x where x is negative.

Actual Behavior

2^(-8) gives inf, and the other negative power leads to a runtime crash:

$zig0.9 test tmp.zig
Test [1/1] test ""... in: -8.0e+00, out: inf
thread 10835 panic: integer overflow
/mnt/c/Users/legaul/Documents/personal/zig/build/lib/zig/std/math/exp2.zig:407:9: 0x217a7c in std.math.exp2.exp2_64 (test)
    i_0 += tblsiz / 2;
        ^
/mnt/c/Users/legaul/Documents/personal/zig/build/lib/zig/std/math/exp2.zig:20:23: 0x2168e6 in std.math.exp2.exp2 (test)
        f64 => exp2_64(x),
                      ^
/home/legaul/tmp/tmp.zig:10:44: 0x21512f in test "" (test)
    print("in: {}, out: {}\n", .{val2, exp2(val2)});
                                           ^
/mnt/c/Users/legaul/Documents/personal/zig/build/lib/zig/std/special/test_runner.zig:77:28: 0x2400a2 in std.special.main (test)
        } else test_fn.func();
                           ^
/mnt/c/Users/legaul/Documents/personal/zig/build/lib/zig/std/start.zig:517:22: 0x234cac in std.start.callMain (test)
            root.main();
                     ^
/mnt/c/Users/legaul/Documents/personal/zig/build/lib/zig/std/start.zig:469:12: 0x2182de in std.start.callMainWithArgs (test)
    return @call(.{ .modifier = .always_inline }, callMain, .{});
           ^
/mnt/c/Users/legaul/Documents/personal/zig/build/lib/zig/std/start.zig:383:17: 0x216e26 in std.start.posixCallMainAndExit (test)
    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));
                ^
/mnt/c/Users/legaul/Documents/personal/zig/build/lib/zig/std/start.zig:296:5: 0x216c32 in std.start._start (test)
    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
    ^
error: the following test command crashed:
zig-cache/o/47b60d05dca7afaac0e6df86d296a7b9/test /mnt/c/Users/legaul/Documents/personal/zig/build/zig

Note that the same problem is not observed for exp() or exp2_32().

@LewisGaul LewisGaul added the bug Observed behavior contradicts documented or intended behavior label Oct 21, 2021
@LewisGaul
Copy link
Contributor Author

I came across this while working on #4026 (currently adding tests). I can work on a fix for this today/tomorrow.

@andrewrk andrewrk added contributor friendly This issue is limited in scope and/or knowledge of Zig internals. standard library This issue involves writing Zig code for the standard library. labels Oct 26, 2021
@andrewrk andrewrk added this to the 0.9.0 milestone Oct 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants