Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@aferreira
Created June 9, 2017 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aferreira/76ca4cccd96028ac82e3fc281c0780a5 to your computer and use it in GitHub Desktop.
Save aferreira/76ca4cccd96028ac82e3fc281c0780a5 to your computer and use it in GitHub Desktop.
_modf() implementations - https://github.com/kraih/mojo/pull/1101
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