Created
June 9, 2017 16:45
-
-
Save aferreira/76ca4cccd96028ac82e3fc281c0780a5 to your computer and use it in GitHub Desktop.
_modf() implementations - https://github.com/kraih/mojo/pull/1101
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use POSIX (); | |
### shorter with POSIX::floor | |
sub _modf { my $t = POSIX::floor($_[0]); ($t, $_[0] - $t) } | |
### with optimization for the common case where argument is integer | |
sub _modf { | |
return ($_[0], 0) if $_[0] == int $_[0]; | |
my $t = POSIX::floor($_[0]); | |
return ($t, $_[0] - $t); | |
} | |
### original | |
# ($t, $f) = _modf($x) | |
# Decompose $x into $t + $f where $t is integer and 0 ≤ $f < 1 | |
sub _modf { | |
return ($_[0], 0) if $_[0] == int $_[0]; | |
my $x = $_[0]; | |
my $t = ($x >= 0) ? int $x : int $x - 1; | |
return ($t, $x - $t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment