Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
inox
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LARA
inox
Commits
2f22bb55
Commit
2f22bb55
authored
9 years ago
by
Regis Blanc
Committed by
Nicolas Voirol
9 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Rational defined using class invariants
parent
741fbbe1
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
library/lang/Rational.scala
+7
-13
7 additions, 13 deletions
library/lang/Rational.scala
with
7 additions
and
13 deletions
library/lang/Rational.scala
+
7
−
13
View file @
2f22bb55
...
@@ -9,72 +9,65 @@ import scala.language.implicitConversions
...
@@ -9,72 +9,65 @@ import scala.language.implicitConversions
@library
@library
case
class
Rational
(
numerator
:
BigInt
,
denominator
:
BigInt
)
{
case
class
Rational
(
numerator
:
BigInt
,
denominator
:
BigInt
)
{
require
(
this
.
isRational
)
def
+
(
that
:
Rational
)
:
Rational
=
{
def
+
(
that
:
Rational
)
:
Rational
=
{
require
(
this
.
isRational
&&
that
.
isRational
)
Rational
(
this
.
numerator
*
that
.
denominator
+
that
.
numerator
*
this
.
denominator
,
this
.
denominator
*
that
.
denominator
)
Rational
(
this
.
numerator
*
that
.
denominator
+
that
.
numerator
*
this
.
denominator
,
this
.
denominator
*
that
.
denominator
)
}
ensuring
(
res
=>
res
.
isRational
)
}
ensuring
(
res
=>
res
.
isRational
)
def
-
(
that
:
Rational
)
:
Rational
=
{
def
-
(
that
:
Rational
)
:
Rational
=
{
require
(
this
.
isRational
&&
that
.
isRational
)
Rational
(
this
.
numerator
*
that
.
denominator
-
that
.
numerator
*
this
.
denominator
,
this
.
denominator
*
that
.
denominator
)
Rational
(
this
.
numerator
*
that
.
denominator
-
that
.
numerator
*
this
.
denominator
,
this
.
denominator
*
that
.
denominator
)
}
ensuring
(
res
=>
res
.
isRational
)
}
ensuring
(
res
=>
res
.
isRational
)
def
unary_-
:
Rational
=
{
def
unary_-
:
Rational
=
{
require
(
this
.
isRational
)
Rational
(-
this
.
numerator
,
this
.
denominator
)
Rational
(-
this
.
numerator
,
this
.
denominator
)
}
ensuring
(
res
=>
res
.
isRational
)
}
ensuring
(
res
=>
res
.
isRational
)
def
*
(
that
:
Rational
)
:
Rational
=
{
def
*
(
that
:
Rational
)
:
Rational
=
{
require
(
this
.
isRational
&&
that
.
isRational
)
Rational
(
this
.
numerator
*
that
.
numerator
,
this
.
denominator
*
that
.
denominator
)
Rational
(
this
.
numerator
*
that
.
numerator
,
this
.
denominator
*
that
.
denominator
)
}
ensuring
(
res
=>
res
.
isRational
)
}
ensuring
(
res
=>
res
.
isRational
)
def
/
(
that
:
Rational
)
:
Rational
=
{
def
/
(
that
:
Rational
)
:
Rational
=
{
require
(
this
.
isRational
&&
that
.
isRational
&&
that
.
nonZero
)
require
(
that
.
nonZero
)
val
newNumerator
=
this
.
numerator
*
that
.
denominator
val
newNumerator
=
this
.
numerator
*
that
.
denominator
val
newDenominator
=
this
.
denominator
*
that
.
numerator
val
newDenominator
=
this
.
denominator
*
that
.
numerator
normalize
(
newNumerator
,
newDenominator
)
normalize
(
newNumerator
,
newDenominator
)
}
ensuring
(
res
=>
res
.
isRational
)
}
ensuring
(
res
=>
res
.
isRational
)
def
reciprocal
:
Rational
=
{
def
reciprocal
:
Rational
=
{
require
(
this
.
isRational
&&
this
.
nonZero
)
require
(
this
.
nonZero
)
normalize
(
this
.
denominator
,
this
.
numerator
)
normalize
(
this
.
denominator
,
this
.
numerator
)
}
ensuring
(
res
=>
res
.
isRational
)
}
ensuring
(
res
=>
res
.
isRational
)
def
~
(
that
:
Rational
)
:
Boolean
=
{
def
~
(
that
:
Rational
)
:
Boolean
=
{
require
(
this
.
isRational
&&
that
.
isRational
)
this
.
numerator
*
that
.
denominator
==
that
.
numerator
*
this
.
denominator
this
.
numerator
*
that
.
denominator
==
that
.
numerator
*
this
.
denominator
}
}
def
<
(
that
:
Rational
)
:
Boolean
=
{
def
<
(
that
:
Rational
)
:
Boolean
=
{
require
(
this
.
isRational
&&
that
.
isRational
)
this
.
numerator
*
that
.
denominator
<
that
.
numerator
*
this
.
denominator
this
.
numerator
*
that
.
denominator
<
that
.
numerator
*
this
.
denominator
}
}
def
<=
(
that
:
Rational
)
:
Boolean
=
{
def
<=
(
that
:
Rational
)
:
Boolean
=
{
require
(
this
.
isRational
&&
that
.
isRational
)
this
.
numerator
*
that
.
denominator
<=
that
.
numerator
*
this
.
denominator
this
.
numerator
*
that
.
denominator
<=
that
.
numerator
*
this
.
denominator
}
}
def
>
(
that
:
Rational
)
:
Boolean
=
{
def
>
(
that
:
Rational
)
:
Boolean
=
{
require
(
this
.
isRational
&&
that
.
isRational
)
this
.
numerator
*
that
.
denominator
>
that
.
numerator
*
this
.
denominator
this
.
numerator
*
that
.
denominator
>
that
.
numerator
*
this
.
denominator
}
}
def
>=
(
that
:
Rational
)
:
Boolean
=
{
def
>=
(
that
:
Rational
)
:
Boolean
=
{
require
(
this
.
isRational
&&
that
.
isRational
)
this
.
numerator
*
that
.
denominator
>=
that
.
numerator
*
this
.
denominator
this
.
numerator
*
that
.
denominator
>=
that
.
numerator
*
this
.
denominator
}
}
def
nonZero
:
Boolean
=
{
def
nonZero
:
Boolean
=
{
require
(
this
.
isRational
)
numerator
!=
0
numerator
!=
0
}
}
def
isRational
:
Boolean
=
denominator
>
0
private
def
isRational
:
Boolean
=
denominator
>
0
private
def
normalize
(
num
:
BigInt
,
den
:
BigInt
)
:
Rational
=
{
private
def
normalize
(
num
:
BigInt
,
den
:
BigInt
)
:
Rational
=
{
require
(
den
!=
0
)
if
(
den
<
0
)
if
(
den
<
0
)
Rational
(-
num
,
-
den
)
Rational
(-
num
,
-
den
)
else
else
...
@@ -88,4 +81,5 @@ object Rational {
...
@@ -88,4 +81,5 @@ object Rational {
implicit
def
bigIntToRat
(
n
:
BigInt
)
:
Rational
=
Rational
(
n
,
1
)
implicit
def
bigIntToRat
(
n
:
BigInt
)
:
Rational
=
Rational
(
n
,
1
)
def
apply
(
n
:
BigInt
)
:
Rational
=
Rational
(
n
,
1
)
def
apply
(
n
:
BigInt
)
:
Rational
=
Rational
(
n
,
1
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment