Skip to content

decimal Literals #8093

Open
Open

Description

@p-98

Description

It would be helpful to have a more concise way to specify numbers of type decimal. JavaScript solved a similar issue with BigInts by using a postfix.

decimal("0.5") // current
0.5d           // proposed

Use Case

We are currently not using decimals in some places it would make sense to, simply because decimal("0.5") is too long and would visually polute the codebase.

Activity

  1. added
    feature request
    on Apr 11, 2026
  2. added
    scripting
    syntax
    on Apr 11, 2026
  3. Lege19 commented on May 14, 2026

    @Lege19
    Contributor

    I've implemented this, I'll open a PR when I've written some tests but it seems to work.

  4. bastienvoirin commented on May 27, 2026

    @bastienvoirin
    SponsorContributor

    IMO this clashes with the existing use of a postfix for (length) units (pt, mm, in...) so I would prefer a more comprehensive extension of postfix: I propose to register either custom constructors which would not only allow 123.456d == decimal("123.456") as proposed here, but also custom units.

    E.g. (defining custom postfix constructors using a leading underscore as a "placeholder", which is orthogonal to my custom constructors proposal and subject to bikeshedding)

    // Custom constructors
    
    #let _d = decimal
    #let x = decimal("0.5") // Current
    #let y = 0.5d // Syntax sugar for `_d("0.5") == decimal("0.5")`
    // Custom units
    
    #let unit(base-value: number, ..dimensions): (number: str) => quantity = {
      let dimensions = dimensions.named()
      let quantity-constructor(number: str) = {
        let number = float(number) // Could be `int`, `float`, `decimal`... To bikeshed or define somewhere as an argument
        if dimensions == (:) {
          number * base-value
        } else {
          (number * base-value, dimensions)
        }
      }
      return quantity-constructor
    }
    
    #let _s = unit(1, T: 1) // unit(base-value, ..dimensions)
    #let _h = unit(3600, T: 1)
    
    #let three-seconds = 3s // Syntax sugar for `_s("3") == (unit(1, T: 1))("3") == (3, (T: 1))`
    #let two-hours = 2h // Syntax sugar for `_h("2") == (unit(3600, T: 1))("2") == (7200, (T: 1))`

    It would be way cleaner and powerful with custom types obviously, and we could even imagine custom operations to make such units much more useful:

    Example implementation
    // E.g. `2h + 3600s == _add_(2h, 3600s) == _add_((7200, T: 1), (3600, T: 1)) == (10800, (T: 1)) == 3h`
    #let _add_((val1, dim1), (val2, dim2)) = {
      if dim1 != dim2 {
        dim1 = dim1.pairs().fold("", (acc, (dim, pow)) => acc + str(dim) + "^" + str(pow))
        dim2 = dim2.pairs().fold("", (acc, (dim, pow)) => acc + str(dim) + "^" + str(pow))
        panic("Cannot add quantities with dimensions " + dim1 + " and " + dim2)
      }
      (val1 + val2, dim1)
    }
    // E.g. `2h - 3600s == _sub_(2h, 3600s) == _sub_((7200, T: 1), (3600, T: 1)) == (3600, (T: 1)) == 1h`
    #let _sub_((val1, dim1), (val2, dim2)) = {
      if dim1 != dim2 {
        dim1 = dim1.pairs().fold("", (acc, (dim, pow)) => acc + str(dim) + "^" + str(pow))
        dim2 = dim2.pairs().fold("", (acc, (dim, pow)) => acc + str(dim) + "^" + str(pow))
        panic("Cannot subtract quantities with dimensions " + dim1 + " and " + dim2)
      }
      (val1 - val2, dim1)
    }
    // E.g. `3m * 4m == _mul_(3m, 4m) == _mul_((3, L: 1), (4, L: 1)) == (12, (L: 2))`
    #let _mul_((val1, dim1), (val2, dim2)) = {
      // Copy dim1 and dim2, reduce common keys with a sum
      let dims = dim1
      for (dim, pow) in dim2.pairs() {
        dims.insert(dim, dims.at(dim, default: 0) + pow)
      }
      let dims = dim1.pairs().filter(((dim, pow)) => pow != 0).to-dict()
    
      if dims == (:) {
        val1 * val2
      } else {
        (val1 * val2, dims)
      }
    }
    // E.g. `72km / 1h == _div_(72km, 1h) == _div_((72000, L: 1), (3600, T: 1)) == (20, (L: 1, T: -1))`
    #let _div_((val1, dim1), (val2, dim2)) = {
      // Copy dim1 and dim2, reduce common keys with a difference
      for (dim, pow) in dim2.pairs() {
        dim1.insert(dim, dim1.at(dim, default: 0) - pow)
      }
      let dims = dim1.pairs().filter(((dim, pow)) => pow != 0).to-dict()
    
      if dims == (:) {
        val1 / val2
      } else {
        (val1 / val2, dims)
      }
    }

    I'll try to open a proper issue but that was the gist in the meantime 😅
    Edit: #8585

  5. bastienvoirin commented on May 27, 2026

    @bastienvoirin
    SponsorContributor

    To be clear: what I disagree with is to implement decimal literals in the compiler as #8288 does, what I propose instead is to allow registering custom postfix syntax sugar in user space.

    This would not only be much more flexible and allow domain-specific postfix notations/units (including for STEM calculations 🥳), but I also dislike the d built-in/compiler postfix in particular because to me it evokes double or even day as much as decimal (I don't think that decimal and the d postfix are that common in scripting languages). A user-space definition of the d postfix for decimal would make the meaning explicit, which IMO is better in this case.

  6. p-98 commented on Jul 18, 2026

    @p-98
    Author

    I generally agree that custom units are useful. However, i don't like the underscore-makes-things-special idea as e.g. tidy already uses underscores to mark private functions.

    But back to this issue: In my opinion, if it is a primitive type, there should be compact compiler-defined syntax for it. Otherwise it feels cumbersome and I'll usually not use it. Even if there was the option to define custom syntax, there should be compiler syntax for it, so that I can look into any codebase and immediately see that it's a decimal.
    I don't necessarily agree that the syntax would be inconsistent with units (a pt turns a double into length and d would turn a double into a decimal), but if that was the dealbreaker we could just prefix the number.

    Regarding d looking like double, if most numbers don't have a pre-/postfix but then a user sees some with d, if think they would just look up what it means.
    Ultimately I think decimal should be default floating point type instead of the binary one because of it is more intuitive but this is a different story :D

  7. linux-user36 commented on Jul 19, 2026

    @linux-user36
    Contributor

    As a side note, C# und F# use m as a suffix to distinguish decimals from doubles. Considering there's already cm and mm though, this might be misinterpreted by readers to mean "meter"...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestscriptingsyntax

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions