gren-lang / gren-lang/core

`^` with a negative exponent puts `0.5` and `Infinity` into an `Int`

Open
#140 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
48
Forks
14
Avg merge
4h 14m
Merged PRs (30d)
1

Description

Repository: gren-lang/core
Found against: gren 0.6.6, gren-lang/core 7.4.2, node 22

Summary

^ is Math.pow with no integer case, so an Int-typed expression evaluates
to a fraction, or to Infinity:

2 ^ -1      --> 0.5         -- typed Int
0 ^ -1      --> Infinity    -- typed Int

Both are reachable with a computed exponent, no cast, no Float anywhere in
the expression, and no warning. String.fromInt on the first prints 0.5.

Reproduction

Operands are computed rather than literal, so this is not constant folding, and
both the inlined and the kernel path are shown, in case the two ever differ as
they do for // (#383):

apply : (Int -> Int -> Int) -> Int -> Int -> Int
apply f a b =
    f a b


two : Int
two =
    String.count "ab"


minusOne : Int
minusOne =
    negate (String.count "a")
expression result
String.fromInt (two ^ minusOne) 0.5
String.fromInt (apply (^) two minusOne) 0.5
String.fromInt ((two - two) ^ minusOne) Infinity

Cause

core/src/Gren/Kernel/Basics.js:

var _Basics_pow = F2(Math.pow);

Math.pow is the float operation. Elm has the same definition and the same
behaviour, so this is inherited rather than introduced.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with core/src/Gren/Kernel/Basics.js and the _Basics_pow definition, then reproduce the three expressions from the issue with the inlined and kernel paths. Determine the intended behavior for negative exponents on Int values; done means those expressions no longer produce fractional or Infinity results while preserving the documented integer typing.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.