| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/DOM/Number.pm | ||
| 3 | ## Version v0.2.0 | ||
| 4 | ## Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2021/12/13 | ||
| 7 | ## Modified 2022/09/18 | ||
| 8 | ## All rights reserved | ||
| 9 | ## | ||
| 10 | ## | ||
| 11 | ## This program is free software; you can redistribute it and/or modify it | ||
| 12 | ## under the same terms as Perl itself. | ||
| 13 | ##---------------------------------------------------------------------------- | ||
| 14 | package HTML::Object::DOM::Number; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( Module::Generic::Number ); | ||
| 21 | use vars qw( $EPSILON $VERSION ); | ||
| 22 | use Config; | ||
| 23 | # use Machine::Epsilon (); | ||
| 24 | use POSIX qw( Inf NaN ); | ||
| 25 | use constant { | ||
| 26 | MAX_SAFE_INTEGER => (POSIX::pow(2,53) - 1), | ||
| 27 | # MAX_VALUE => (1.7976931348623157 * POSIX::pow(10,308)), | ||
| 28 | MAX_VALUE => (POSIX::pow(2, $Config::Config{use64bitint} eq 'define' ? 64 : 32)), | ||
| 29 | MIN_SAFE_INTEGER => (-(POSIX::pow(2,53) - 1)), | ||
| 30 | MIN_VALUE => POSIX::pow(2, -1074), | ||
| 31 | # <https://perldoc.perl.org/perldata#Special-floating-point:-infinity-(Inf)-and-not-a-number-(NaN)> | ||
| 32 | # NEGATIVE_INFINITY => -9**9**9, | ||
| 33 | NEGATIVE_INFINITY => -Inf, | ||
| 34 | POSITIVE_INFINITY => Inf, | ||
| 35 | }; | ||
| 36 | our $EPSILON; | ||
| 37 | our $VERSION = 'v0.2.0'; | ||
| 38 | }; | ||
| 39 | |||
| 40 | use strict; | ||
| 41 | use warnings; | ||
| 42 | |||
| 43 | sub init | ||
| 44 | { | ||
| 45 | my $self = shift( @_ ); | ||
| 46 | $self->{_init_strict_use_sub} = 1; | ||
| 47 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 48 | # $EPSILON = Machine::Epsilon::machine_epsilon() if( !defined( $EPSILON ) ); | ||
| 49 | $EPSILON = POSIX::pow(2,-52) if( !defined( $EPSILON ) ); | ||
| 50 | return( $self ); | ||
| 51 | } | ||
| 52 | |||
| 53 | # Note: property EPSILON | ||
| 54 | sub EPSILON : lvalue { return( shift->_lvalue({ | ||
| 55 | get => sub | ||
| 56 | { | ||
| 57 | my $self = shift( @_ ); | ||
| 58 | return( $EPSILON ); | ||
| 59 | }, | ||
| 60 | set => sub | ||
| 61 | { | ||
| 62 | my( $self, $ref ) = @_; | ||
| 63 | $EPSILON = shift( @$ref ); | ||
| 64 | } | ||
| 65 | }, @_ ) ); } | ||
| 66 | |||
| 67 | # Note: property MAX_SAFE_INTEGER is a constant | ||
| 68 | |||
| 69 | # Note: property MAX_VALUE is a constant | ||
| 70 | |||
| 71 | # Note: property MIN_SAFE_INTEGER is a constant | ||
| 72 | |||
| 73 | # Note: property MIN_VALUE is a constant | ||
| 74 | |||
| 75 | # Note: property NEGATIVE_INFINITY is a constant | ||
| 76 | |||
| 77 | # Note: property POSITIVE_INFINITY is a constant | ||
| 78 | |||
| 79 | sub isFinite { return( $_[1] != Inf ); } | ||
| 80 | |||
| 81 | sub isInteger { return( shift->_is_integer( $_[0] ) ); } | ||
| 82 | |||
| 83 | sub isNaN { return( POSIX::isnan( $_[1] ) ); } | ||
| 84 | |||
| 85 | sub isSafeInteger { return( $_[0]->_is_integer( $_[1] ) && abs( $_[1] ) < MAX_SAFE_INTEGER ); } | ||
| 86 | |||
| 87 | sub parseFloat { return( shift->new( shift( @_ ) ) ); } | ||
| 88 | |||
| 89 | sub parseInt { return( shift->new( shift( @_ ) ) ); } | ||
| 90 | |||
| 91 | 1; | ||
| 92 | # NOTE: POD | ||
| 93 | __END__ | ||
| 94 | |||
| 95 | =encoding utf-8 | ||
| 96 | |||
| 97 | =head1 NAME | ||
| 98 | |||
| 99 | HTML::Object::DOM::Number - HTML Object DOM Number | ||
| 100 | |||
| 101 | =head1 SYNOPSIS | ||
| 102 | |||
| 103 | use HTML::Object::DOM::Number; | ||
| 104 | my $this = HTML::Object::DOM::Number->new || | ||
| 105 | die( HTML::Object::DOM::Number->error, "\n" ); | ||
| 106 | |||
| 107 | =head1 VERSION | ||
| 108 | |||
| 109 | v0.2.0 | ||
| 110 | |||
| 111 | =head1 DESCRIPTION | ||
| 112 | |||
| 113 | This interface represents and manipulates numbers like 37 or -9.25. | ||
| 114 | |||
| 115 | It inherits from L<Module::Generic::Number> | ||
| 116 | |||
| 117 | =head1 PROPERTIES | ||
| 118 | |||
| 119 | =head2 EPSILON | ||
| 120 | |||
| 121 | The smallest interval between two representable numbers. | ||
| 122 | |||
| 123 | The EPSILON property has a value of approximately 2.22044604925031e-16, or 2^-52 | ||
| 124 | |||
| 125 | You do not have to create a HTML::Object::DOM::Number object to access this static property (use HTML::Object::DOM::Number->EPSILON). | ||
| 126 | |||
| 127 | Example: | ||
| 128 | |||
| 129 | my $result = abs(0.2 - 0.3 + 0.1); | ||
| 130 | |||
| 131 | say $result; | ||
| 132 | # expected output: 2.77555756156289e-17 | ||
| 133 | |||
| 134 | say $result < HTML::Object::DOM::Number->EPSILON; | ||
| 135 | # expected output: 1 (i.e. true) | ||
| 136 | |||
| 137 | if( !defined( HTML::Object::DOM::Number->EPSILON ) ) | ||
| 138 | { | ||
| 139 | HTML::Object::DOM::Number->EPSILON = POSIX::pow(2, -52); | ||
| 140 | } | ||
| 141 | |||
| 142 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/EPSILON> | ||
| 143 | |||
| 144 | =head2 MAX_SAFE_INTEGER | ||
| 145 | |||
| 146 | This represents the maximum safe integer in JavaScript i.e. (2^53 - 1). | ||
| 147 | |||
| 148 | However, under perl, it does not work the same way. | ||
| 149 | |||
| 150 | Example: | ||
| 151 | |||
| 152 | Under JavaScript | ||
| 153 | |||
| 154 | const x = Number.MAX_SAFE_INTEGER + 1; | ||
| 155 | const y = Number.MAX_SAFE_INTEGER + 2; | ||
| 156 | |||
| 157 | console.log(Number.MAX_SAFE_INTEGER); | ||
| 158 | // expected output: 9007199254740991 | ||
| 159 | |||
| 160 | console.log(x); | ||
| 161 | // expected output: 9007199254740992 | ||
| 162 | |||
| 163 | console.log(x === y); | ||
| 164 | // expected output: true | ||
| 165 | |||
| 166 | However, under perl, C<$x == $y> would be false of course. | ||
| 167 | |||
| 168 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER> | ||
| 169 | |||
| 170 | =head2 MAX_VALUE | ||
| 171 | |||
| 172 | This represents the maximum numeric value representable in JavaScript. | ||
| 173 | |||
| 174 | The C<MAX_VALUE> property has a value of approximately 1.79E+308, or 2^64. Values larger than MAX_VALUE are represented as Infinity. | ||
| 175 | |||
| 176 | Because MAX_VALUE is a static property of HTML::Object::DOM::Number, you always use it as C<HTML::Object::DOM::Number->MAX_VALUE>, rather than as a property of a C<HTML::Object::DOM::Number> object you created. | ||
| 177 | |||
| 178 | Example: | ||
| 179 | |||
| 180 | if( $num1 * $num2 <= HTML::Object::DOM::Number->MAX_VALUE ) | ||
| 181 | { | ||
| 182 | func1(); | ||
| 183 | } | ||
| 184 | else | ||
| 185 | { | ||
| 186 | func2(); | ||
| 187 | } | ||
| 188 | |||
| 189 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_VALUE> | ||
| 190 | |||
| 191 | =head2 MIN_SAFE_INTEGER | ||
| 192 | |||
| 193 | This represents the minimum safe integer in JavaScript (-(2^53 - 1)). | ||
| 194 | |||
| 195 | Because C<MIN_SAFE_INTEGER> is a static property of HTML::Object::DOM::Number, you can use it as C<HTML::Object::DOM::Number->MIN_SAFE_INTEGER>, rather than as a property of a C<HTML::Object::DOM::Number> object you created. | ||
| 196 | |||
| 197 | Example: | ||
| 198 | |||
| 199 | HTML::Object::DOM::Number->MIN_SAFE_INTEGER # -9007199254740991 | ||
| 200 | -(POSIX::pow(2, 53) - 1) # -9007199254740991 | ||
| 201 | |||
| 202 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_SAFE_INTEGER> | ||
| 203 | |||
| 204 | =head2 MIN_VALUE | ||
| 205 | |||
| 206 | The smallest positive representable number-that is, the positive number closest to zero (without actually being zero). | ||
| 207 | |||
| 208 | Example: | ||
| 209 | |||
| 210 | if( $num1 / $num2 >= HTML::Object::DOM::Number->MIN_VALUE ) | ||
| 211 | { | ||
| 212 | func1(); | ||
| 213 | } | ||
| 214 | else | ||
| 215 | { | ||
| 216 | func2(); | ||
| 217 | } | ||
| 218 | |||
| 219 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_VALUE> | ||
| 220 | |||
| 221 | =head2 NEGATIVE_INFINITY | ||
| 222 | |||
| 223 | Special value representing negative infinity. Returned on overflow. | ||
| 224 | |||
| 225 | Example: | ||
| 226 | |||
| 227 | my $smallNumber = (-HTML::Object::DOM::Number->MAX_VALUE) * 2; | ||
| 228 | |||
| 229 | if( $smallNumber == HTML::Object::DOM::Number->NEGATIVE_INFINITY ) | ||
| 230 | { | ||
| 231 | $smallNumber = returnFinite(); | ||
| 232 | } | ||
| 233 | |||
| 234 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/NEGATIVE_INFINITY> | ||
| 235 | |||
| 236 | =head2 NaN | ||
| 237 | |||
| 238 | Special "Not a Number" value. | ||
| 239 | |||
| 240 | This is actually a value exported by L<POSIX> | ||
| 241 | |||
| 242 | Example: | ||
| 243 | |||
| 244 | sub sanitise | ||
| 245 | { | ||
| 246 | my $x = shift( @_ ); | ||
| 247 | if( isNaN($x) ) | ||
| 248 | { | ||
| 249 | return( HTML::Object::DOM::Number->NaN ); | ||
| 250 | } | ||
| 251 | return($x); | ||
| 252 | } | ||
| 253 | |||
| 254 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/NaN> | ||
| 255 | |||
| 256 | =head2 POSITIVE_INFINITY | ||
| 257 | |||
| 258 | Special value representing infinity. Returned on overflow. | ||
| 259 | |||
| 260 | Example: | ||
| 261 | |||
| 262 | my $bigNumber = HTML::Object::DOM::Number->MAX_VALUE * 2; | ||
| 263 | |||
| 264 | if( $bigNumber == HTML::Object::DOM::Number->POSITIVE_INFINITY ) | ||
| 265 | { | ||
| 266 | $bigNumber = returnFinite(); | ||
| 267 | } | ||
| 268 | |||
| 269 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/POSITIVE_INFINITY> | ||
| 270 | |||
| 271 | =head1 METHODS | ||
| 272 | |||
| 273 | Inherits methods from its parent L<Module::Generic::Number> | ||
| 274 | |||
| 275 | =head2 isFinite | ||
| 276 | |||
| 277 | Determine whether the passed value is a finite number. | ||
| 278 | |||
| 279 | Example: | ||
| 280 | |||
| 281 | HTML::Object::DOM::Number->isFinite($value) | ||
| 282 | |||
| 283 | HTML::Object::DOM::Number->isFinite(Infinity); # false | ||
| 284 | HTML::Object::DOM::Number->isFinite(NaN); # false | ||
| 285 | HTML::Object::DOM::Number->isFinite(-Infinity); # false | ||
| 286 | |||
| 287 | HTML::Object::DOM::Number->isFinite(0); # true | ||
| 288 | HTML::Object::DOM::Number->isFinite(2e64); # true | ||
| 289 | |||
| 290 | HTML::Object::DOM::Number->isFinite('0'); # false, would've been true with | ||
| 291 | # global isFinite('0') | ||
| 292 | HTML::Object::DOM::Number->isFinite(undef); # false, would've been true with | ||
| 293 | # global isFinite(undef) | ||
| 294 | |||
| 295 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite> | ||
| 296 | |||
| 297 | =head2 isInteger | ||
| 298 | |||
| 299 | Determine whether the passed value is an integer. | ||
| 300 | |||
| 301 | Example: | ||
| 302 | |||
| 303 | HTML::Object::DOM::Number->isInteger(value) | ||
| 304 | |||
| 305 | HTML::Object::DOM::Number->isInteger(0); # true | ||
| 306 | HTML::Object::DOM::Number->isInteger(1); # true | ||
| 307 | HTML::Object::DOM::Number->isInteger(-100000); # true | ||
| 308 | HTML::Object::DOM::Number->isInteger(99999999999999999999999); # true | ||
| 309 | |||
| 310 | HTML::Object::DOM::Number->isInteger(0.1); # false | ||
| 311 | HTML::Object::DOM::Number->isInteger(Math->PI); # false | ||
| 312 | |||
| 313 | HTML::Object::DOM::Number->isInteger(NaN); # false | ||
| 314 | HTML::Object::DOM::Number->isInteger(Infinity); # false | ||
| 315 | HTML::Object::DOM::Number->isInteger(-Infinity); # false | ||
| 316 | HTML::Object::DOM::Number->isInteger('10'); # false | ||
| 317 | HTML::Object::DOM::Number->isInteger(true); # false | ||
| 318 | HTML::Object::DOM::Number->isInteger(false); # false | ||
| 319 | HTML::Object::DOM::Number->isInteger([1]); # false | ||
| 320 | |||
| 321 | HTML::Object::DOM::Number->isInteger(5.0); # true | ||
| 322 | HTML::Object::DOM::Number->isInteger(5.000000000000001); # false | ||
| 323 | HTML::Object::DOM::Number->isInteger(5.0000000000000001); # true | ||
| 324 | |||
| 325 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger> | ||
| 326 | |||
| 327 | =head2 isNaN | ||
| 328 | |||
| 329 | Determine whether the passed value is C<NaN>. | ||
| 330 | |||
| 331 | Example: | ||
| 332 | |||
| 333 | HTML::Object::DOM::Number->isNaN(value) | ||
| 334 | |||
| 335 | HTML::Object::DOM::Number->isNaN(NaN); # true | ||
| 336 | HTML::Object::DOM::Number->isNaN(HTML::Object::DOM::Number->NaN); # true | ||
| 337 | HTML::Object::DOM::Number->isNaN(0 / 0); # true | ||
| 338 | |||
| 339 | # e->g. these would have been true with global isNaN() | ||
| 340 | HTML::Object::DOM::Number->isNaN('NaN'); # false | ||
| 341 | HTML::Object::DOM::Number->isNaN(undefined); # false | ||
| 342 | HTML::Object::DOM::Number->isNaN({}); # false | ||
| 343 | HTML::Object::DOM::Number->isNaN('blabla'); # false | ||
| 344 | |||
| 345 | # These all return false | ||
| 346 | HTML::Object::DOM::Number->isNaN(true); | ||
| 347 | HTML::Object::DOM::Number->isNaN(undef); | ||
| 348 | HTML::Object::DOM::Number->isNaN(37); | ||
| 349 | HTML::Object::DOM::Number->isNaN('37'); | ||
| 350 | HTML::Object::DOM::Number->isNaN('37.37'); | ||
| 351 | HTML::Object::DOM::Number->isNaN(''); | ||
| 352 | HTML::Object::DOM::Number->isNaN(' '); | ||
| 353 | |||
| 354 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN> | ||
| 355 | |||
| 356 | =head2 isSafeInteger | ||
| 357 | |||
| 358 | Determine whether the passed value is a safe integer (number between -(2^53 - 1) and 2^53 - 1). | ||
| 359 | |||
| 360 | Example: | ||
| 361 | |||
| 362 | HTML::Object::DOM::Number->isSafeInteger(3); # true | ||
| 363 | HTML::Object::DOM::Number->isSafeInteger(POSIX::pow(2, 53)); # false | ||
| 364 | HTML::Object::DOM::Number->isSafeInteger(POSIX::pow(2, 53) - 1); # true | ||
| 365 | HTML::Object::DOM::Number->isSafeInteger(NaN); # false | ||
| 366 | HTML::Object::DOM::Number->isSafeInteger(Infinity); # false | ||
| 367 | HTML::Object::DOM::Number->isSafeInteger('3'); # false | ||
| 368 | HTML::Object::DOM::Number->isSafeInteger(3.1); # false | ||
| 369 | HTML::Object::DOM::Number->isSafeInteger(3.0); # true | ||
| 370 | |||
| 371 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger> | ||
| 372 | |||
| 373 | =head2 parseFloat | ||
| 374 | |||
| 375 | Provided with a value and this will return a new L<HTML::Object::DOM::Number> object. | ||
| 376 | |||
| 377 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/parseFloat> | ||
| 378 | |||
| 379 | =head2 parseInt | ||
| 380 | |||
| 381 | Provided with a value and this will return a new L<HTML::Object::DOM::Number> object. | ||
| 382 | |||
| 383 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/parseInt> | ||
| 384 | |||
| 385 | =head1 AUTHOR | ||
| 386 | |||
| 387 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 388 | |||
| 389 | =head1 SEE ALSO | ||
| 390 | |||
| 391 | L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number>, L<Machine::Epsilon> | ||
| 392 | |||
| 393 | =head1 COPYRIGHT & LICENSE | ||
| 394 | |||
| 395 | Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 396 | |||
| 397 | All rights reserved | ||
| 398 | |||
| 399 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 400 | |||
| 401 | =cut |