Commit | Line | Data |
---|---|---|

a0d0e21e LW |
1 | package integer; |

2 | ||

f06db76b AD |
3 | =head1 NAME |

4 | ||

5 | integer - Perl pragma to compute arithmetic in integer instead of double | |

6 | ||

7 | =head1 SYNOPSIS | |

8 | ||

9 | use integer; | |

10 | $x = 10/3; | |

11 | # $x is now 3, not 3.33333333333333333 | |

12 | ||

13 | =head1 DESCRIPTION | |

14 | ||

a3cb178b | 15 | This tells the compiler to use integer operations |

f06db76b AD |
16 | from here to the end of the enclosing BLOCK. On many machines, |

17 | this doesn't matter a great deal for most computations, but on those | |

18 | without floating point hardware, it can make a big difference. | |

19 | ||

a3cb178b GS |
20 | Note that this affects the operations, not the numbers. If you run this |

21 | code | |

22 | ||

23 | use integer; | |

24 | $x = 1.5; | |

25 | $y = $x + 1; | |

26 | $z = -1.5; | |

27 | ||

28 | you'll be left with C<$x == 1.5>, C<$y == 2> and C<$z == -1>. The $z | |

29 | case happens because unary C<-> counts as an operation. | |

30 | ||

47f6b1df GS |
31 | Native integer arithmetic (as provided by your C compiler) is used. |

32 | This means that Perl's own semantics for arithmetic operations may | |

33 | not be preserved. One common source of trouble is the modulus of | |

34 | negative numbers, which Perl does one way, but your hardware may do | |

35 | another. | |

36 | ||

37 | % perl -le 'print (4 % -3)' | |

38 | -2 | |

39 | % perl -Minteger -le 'print (4 % -3)' | |

40 | 1 | |

41 | ||

f06db76b AD |
42 | See L<perlmod/Pragmatic Modules>. |

43 | ||

44 | =cut | |

45 | ||

a0d0e21e LW |
46 | sub import { |

47 | $^H |= 1; | |

48 | } | |

49 | ||

50 | sub unimport { | |

51 | $^H &= ~1; | |

52 | } | |

53 | ||

54 | 1; |