Browse Source

Add radian-degree conversion

pull/2/head
Arthur POULET 4 years ago
parent
commit
b955ef06dd
No known key found for this signature in database GPG Key ID: FC154EA63FF5D8BC
  1. 11
      README.md
  2. 15
      spec/math/angle.cr
  3. 14
      src/lib/math/angles.cr

11
README.md

@ -46,7 +46,8 @@ Math.binomial_distribution(
### Binomial coefficient
```crystal
Math.coef_binomial(5, 2) # => 10
Math.coef_binomial(5, 2) # =>
10
```
### Factorial
@ -55,6 +56,14 @@ Math.coef_binomial(5, 2) # => 10
Math.factorial(4) # => 24
```
### Radian and degree
```crystal
90.radian # => (Math::PI / 2)
(Math::PI / 4) # => 45.0
```
## Development
- The lib is adapted to be usable with BigInt and BigFloat values

15
spec/math/angle.cr

@ -0,0 +1,15 @@
describe Math::Angle do
it "verify radian to degree" do
45.radian.should eq(Math::PI / 4)
90.radian.should eq(Math::PI / 2)
180.radian.should eq(Math::PI)
360.radian.should eq(Math::PI * 2)
end
it "verify degree to radian" do
(Math::PI / 4).degree.should eq(45)
(Math::PI / 2).degree.should eq(90)
(Math::PI).degree.should eq(180)
(Math::PI * 2).degree.should eq(360)
end
end

14
src/lib/math/angles.cr

@ -0,0 +1,14 @@
module Math::Angle
# Convert degree to radian
def radian
(Math::PI * self) / 180
end
def degree
(self * 180) / (Math::PI)
end
end
abstract struct Number
include Math::Angle
end
Loading…
Cancel
Save