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

Add the modf routine to the core language #462

Open
tbrowder opened this issue Jan 23, 2025 · 2 comments
Open

Add the modf routine to the core language #462

tbrowder opened this issue Jan 23, 2025 · 2 comments
Labels
language Changes to the Raku Programming Language

Comments

@tbrowder
Copy link
Member

tbrowder commented Jan 23, 2025

Introduction

The Raku core library does not have a 'modf' routine. The routine is commonly found in many popular programming languages (including Python), but it was not included in the original Raku release.

I once proposed adding one and made an elementary start at it, but soon realized it is not a trivial task due to numerical issues (see closed Rakudo PR #4432). A proper implementation in Raku would use the power inherent in the language to handle very large numbers, and the returned parts should be correct for those numbers as input to 'modf'. The routine proposed in PR #4432 followed the basic implementation as found in C and C++ with some slight modifications to take advantage of existing Raku features. PR #4432 also included tests.

In the rest of this article I will present my current view on implementing the routine from the user's perspective.

General definition of modf in other languages

  1. Given a number, return the integral and fractional parts of the number (both parts as base 10 values).

  2. If the number is an integer, return zero as the fractional part.

  3. Both parts will have the sign of the input.

Additional features in Raku

  1. The returned values will be in a List of two allomorphs.

  2. There will be two optional parameters to allow the caller to shape the return values.

Signature

sub modf($x, $digits?, :$shape --> List) {...}

where

  • $x - input number (as a number, allomorph, or string)
  • $digits - number of decimal places to show (pad with zeroes on the
    end if needed, or round to the desired precision)
  • :$shape - shape the fractional output by removing any sign and removing
    a decimal point and all following zeroes for zero values

Examples

The following table shows various input formats and what is expected
to be returned. Although not evident in the table, the allomorph
output values will be bracketed by the appropriate quoting style to
retain the string appearance as shown.

Input Digits? Integral Part Fractional Part Shaped Fractional Part
1.2 1 0.2 NC*
-1 -1 -0.0 0
1e-10 10 0 0.0000000001 NC*
-0x10.1 -16 -0.0625 0.0625
12 12 0.0 0

NC - no change from the default appearance

@tbrowder tbrowder added the language Changes to the Raku Programming Language label Jan 23, 2025
@raiph
Copy link

raiph commented Jan 24, 2025

Have you got an opinion on what "core" would mean?

For example, almost all "core" Raku routines are part of CORE, are compiled into the raku interpreter/compiler binary, and allow calls to be done without needing an explicit package import and qualification. For example, say.

A few routines are part of "core" but not part of CORE. They are not compiled into raku, and calls require an explicit package import. For example, plan which requires something like use Test; plan ....

Other possibilities seem plausible. There could be routines, whether part of CORE or just "core", that require explicit import and/or qualification.


To compare the Raku and Python takes that seem particularly relevant to modf:

  • In the Raku world there's CORE. This comprises a few hundred packages that are compiled into raku. Afaik there's currently just one "core" non-CORE package in standard Raku (the language, as against the compiler), namely Test, and one more additional package in standard Rakudo (the standard compiler, as against the standard language), namely Telemetry. (Rakudo Star bundles a ton more, but I presume we sure don't want all of them in "core" Raku, and probably don't want any.)

  • In the Python world the modf routine is a function provided in the Python standard libraries collection's math library, which is one of a few hundred packages. Afaik few or none of these libraries are compiled into the python executable, and an import statement is required if code is to be able to access a given library's functions, and a qualification is required to call a function (i.e. one writes math.modf unless the import statement covers the role of qualification in a longer form as from math import modf).


Perhaps we might best be ultra conservative about expanding CORE, and, conversely, open to adding more "core" but non-CORE packages?

@tbrowder
Copy link
Member Author

tbrowder commented Jan 24, 2025

Yes, I do understand the difference, and my wish was always for it to be in the core language. Larry intentionally intended the language to be as large as it needs to be, and I believe 'modf' was over looked in the beginning. And that has been one of the strengths of Raku over Python: most everything you need is already there.

@lizmat lizmat changed the title Add the 'modf' routine to the core language Add the modf routine to the core language Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
language Changes to the Raku Programming Language
Projects
None yet
Development

No branches or pull requests

2 participants