Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SDA
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
Julien David
SDA
Commits
80e299b3
Commit
80e299b3
authored
5 years ago
by
david
Browse files
Options
Downloads
Patches
Plain Diff
BigDecimal in JAVA
parent
b585423b
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
C/analyzer.c
+2
-2
2 additions, 2 deletions
C/analyzer.c
C/analyzer.h
+2
-2
2 additions, 2 deletions
C/analyzer.h
CPP/analyzer.hpp
+2
-2
2 additions, 2 deletions
CPP/analyzer.hpp
Java/Analyzer.java
+23
-17
23 additions, 17 deletions
Java/Analyzer.java
with
29 additions
and
23 deletions
C/analyzer.c
+
2
−
2
View file @
80e299b3
...
...
@@ -7,7 +7,7 @@ analyzer_t * analyzer_create(){
analyzer_t
*
res
=
(
analyzer_t
*
)
malloc
(
sizeof
(
analyzer_t
)
);
res
->
capacity
=
4
;
res
->
cost
=
(
double
*
)
malloc
(
sizeof
(
double
)
*
res
->
capacity
);
res
->
cumulative_cost
=
(
double
*
)
malloc
(
sizeof
(
double
)
*
res
->
capacity
);
res
->
cumulative_cost
=
(
long
double
*
)
malloc
(
sizeof
(
long
double
)
*
res
->
capacity
);
res
->
cumulative_square
=
0
.;
res
->
size
=
0
;
return
res
;
...
...
@@ -26,7 +26,7 @@ void analyzer_append(analyzer_t * a, double x){
if
(
a
->
size
>=
(
a
->
capacity
*
3
)
/
4
){
a
->
capacity
*=
2
;
a
->
cost
=
(
double
*
)
realloc
(
a
->
cost
,
sizeof
(
double
)
*
a
->
capacity
*
2
);
a
->
cumulative_cost
=
(
double
*
)
realloc
(
a
->
cumulative_cost
,
sizeof
(
double
)
*
a
->
capacity
*
2
);
a
->
cumulative_cost
=
(
long
double
*
)
realloc
(
a
->
cumulative_cost
,
sizeof
(
long
double
)
*
a
->
capacity
*
2
);
}
a
->
cost
[
a
->
size
]
=
x
;
a
->
cumulative_cost
[
a
->
size
]
=
(
a
->
size
)
?
a
->
cumulative_cost
[
a
->
size
-
1
]
+
x
:
x
;
...
...
This diff is collapsed.
Click to expand it.
C/analyzer.h
+
2
−
2
View file @
80e299b3
...
...
@@ -12,9 +12,9 @@ typedef struct analyzer_s{
double
*
cost
;
// Coût cumulatif. La case i contient la somme des coûts des i premières opérations.
// Permet de calculer le coût amorti d'une opération.
double
*
cumulative_cost
;
long
double
*
cumulative_cost
;
// Carré du coût cumulatif. Sert à calculer la variance. On ne garde que la dernière valeur.
double
cumulative_square
;
long
double
cumulative_square
;
// Capacité de stockage des tableaux
size_t
capacity
;
// Nombre d'éléments dans chaque tableaux.
...
...
This diff is collapsed.
Click to expand it.
CPP/analyzer.hpp
+
2
−
2
View file @
80e299b3
...
...
@@ -85,9 +85,9 @@ private:
std
::
vector
<
double
>
cost
;
// Coût cumulatif. La case i contient la somme des coûts des i premières opérations.
// Permet de calculer le coût amorti d'une opération.
std
::
vector
<
double
>
cumulative_cost
;
std
::
vector
<
long
double
>
cumulative_cost
;
// Carré du coût cumulatif. Sert à calculer la variance. On ne garde que la dernière valeur.
double
cumulative_square
;
long
double
cumulative_square
;
};
...
...
This diff is collapsed.
Click to expand it.
Java/Analyzer.java
+
23
−
17
View file @
80e299b3
import
java.io.*
;
import
java.math.BigDecimal
;
import
java.math.RoundingMode
;
import
java.util.ArrayList
;
/**
...
...
@@ -13,8 +15,8 @@ public class Analyzer {
*/
public
Analyzer
()
{
cost
=
new
ArrayList
<
Double
>();
cumulative_cost
=
new
ArrayList
<
Double
>();
cumulative_square
=
0
.;
cumulative_cost
=
new
ArrayList
<
BigDecimal
>();
cumulative_square
=
new
BigDecimal
(
0
.
)
;
}
/**
...
...
@@ -25,9 +27,11 @@ public class Analyzer {
@param x est la valeur que l'on souhaite ajouter à l'analyse.
*/
void
append
(
double
x
){
BigDecimal
x_big
=
new
BigDecimal
(
x
);
cost
.
add
(
x
);
cumulative_cost
.
add
(
(!
cumulative_cost
.
isEmpty
())
?
cumulative_cost
.
get
(
cumulative_cost
.
size
()-
1
)+
x
:
x
);
cumulative_square
+=
x
*
x
;
cumulative_cost
.
add
(
(!
cumulative_cost
.
isEmpty
())
?
cumulative_cost
.
get
(
cumulative_cost
.
size
()-
1
).
add
(
x_big
)
:
x_big
);
BigDecimal
x_square
=
x_big
.
multiply
(
x_big
);
cumulative_square
=
cumulative_square
.
add
(
x_square
);
}
/**
...
...
@@ -35,7 +39,7 @@ public class Analyzer {
Complexité en temps/espace, meilleur cas : O(1)
@returns la somme des coûts enregistrés dans cette analyse.
*/
double
get_total_cost
(){
BigDecimal
get_total_cost
(){
return
cumulative_cost
.
get
(
cumulative_cost
.
size
()-
1
);
}
...
...
@@ -45,8 +49,8 @@ public class Analyzer {
@param pos est l'indice de l'opération pour laquelle on veut connaître le coût amorti.
@returns le coût amorti d'une opération.
*/
double
get_amortized_cost
(
int
pos
){
return
(
pos
>
0
)?
cumulative_cost
.
get
(
pos
)
/
pos
:
cumulative_cost
.
get
(
pos
);
BigDecimal
get_amortized_cost
(
int
pos
){
return
(
pos
>
0
)?
cumulative_cost
.
get
(
pos
)
.
divide
(
new
BigDecimal
(
pos
),
RoundingMode
.
HALF_UP
)
:
cumulative_cost
.
get
(
pos
);
}
/**
...
...
@@ -54,10 +58,10 @@ public class Analyzer {
Complexité en temps/espace, meilleur cas : O(1)
@returns la moyenne des coûts de toutes les opérations enregistrées dans l'analyse.
*/
double
get_average_cost
(){
BigDecimal
get_average_cost
(){
if
(
cumulative_cost
.
isEmpty
())
throw
new
RuntimeException
(
"List is empty"
);
return
cumulative_cost
.
get
(
cumulative_cost
.
size
()-
1
)
/
cumulative_cost
.
size
();
return
cumulative_cost
.
get
(
cumulative_cost
.
size
()-
1
)
.
divide
(
new
BigDecimal
(
cumulative_cost
.
size
()
))
;
}
/**
...
...
@@ -65,11 +69,13 @@ public class Analyzer {
Complexité en temps/espace, meilleur cas : O(1)
@returns la variance des coûts de toutes les opérations enregistrées dans l'analyse.
*/
double
get_variance
(){
double
mean
,
mean_square
;
BigDecimal
get_variance
(){
BigDecimal
mean
,
mean_square
;
mean
=
get_average_cost
();
mean_square
=
mean
*
mean
;
return
cumulative_square
-
mean_square
;
mean_square
=
mean
.
multiply
(
mean
);
if
(
cumulative_square
.
compareTo
(
mean_square
)
<
0
)
throw
new
RuntimeException
(
"Error: mean of squares is less than square of mean: "
+
mean
+
" "
+
cumulative_square
);
return
cumulative_square
.
subtract
(
mean_square
);
}
/**
...
...
@@ -77,8 +83,8 @@ public class Analyzer {
Complexité en temps/espace, meilleur cas : O(1)
@returns l'écart-type des coûts de toutes les opérations enregistrées dans l'analyse.
*/
double
get_standard_deviation
(){
return
Math
.
sqrt
(
get_variance
());
BigDecimal
get_standard_deviation
(){
return
new
BigDecimal
(
Math
.
sqrt
(
get_variance
()
.
doubleValue
())
);
}
/**
...
...
@@ -124,9 +130,9 @@ public class Analyzer {
private
ArrayList
<
Double
>
cost
;
// Coût cumulatif. La case i contient la somme des coûts des i premières opérations.
// Permet de calculer le coût amorti d'une opération.
private
ArrayList
<
Double
>
cumulative_cost
;
private
ArrayList
<
BigDecimal
>
cumulative_cost
;
// Carré du coût cumulatif. Sert à calculer la variance. On ne garde que la dernière valeur.
private
double
cumulative_square
;
private
BigDecimal
cumulative_square
;
}
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