-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
329 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
Copyright 2024 Massimo Santini | ||
This file is part of "Programmazione 2 @ UniMI" teaching material. | ||
This is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This material is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this file. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package it.unimi.di.prog2.h06; | ||
|
||
/** Classe di utilità per il calcolo delle radici quadrate. */ | ||
public class Radici { | ||
|
||
/** . */ | ||
private Radici() {} | ||
|
||
// Nota: Usando {@literal \( ... \)} è possibile includere LaTeX nella documentazione | ||
|
||
/** | ||
* Estrae se possibile la radice quadrata del numero dato. | ||
* | ||
* <p>Funzione <em>parziale</em> che qualora l'argomento sia non negativo restituisce una | ||
* approssimazione {@literal \( y \)} della radice quadrata nel senso che {@literal \( |y^2 - x| < | ||
* 10^{-3} \)}. | ||
* | ||
* @param x il numero di cui estrarre la radice, deve essere non negativo. | ||
* @return l'approssimazione della radice quadrata. | ||
*/ | ||
public static double radiceParziale(double x) { | ||
|
||
// L'implementazione si basa sul metodo di bisezione | ||
// https://en.wikipedia.org/wiki/Bisection_method | ||
|
||
double low = 0, high = x > 1 ? x : 1, mid = -1; | ||
while (high - low > .00001) { | ||
mid = (high + low) / 2; | ||
if (mid * mid - x < 0) low = mid; | ||
else high = mid; | ||
} | ||
return mid; | ||
} | ||
|
||
/** | ||
* Estrae la radice quadrata del numero dato. | ||
* | ||
* <p>Funzione <em>totale</em> che qualora l'argomento sia non negativo restituisce una | ||
* approssimazione {@literal \( y \)} della radice quadrata nel senso che {@literal \( |y^2 - x| < | ||
* 10^{-3} \)}; viceversa, a fronte di un argomento negativo, resituisce il valore convenzionale | ||
* -1. | ||
* | ||
* @param x il numero di cui estrarre la radice. | ||
* @return l'approssimazione della radice quadrata se x è non negativo, altrimenti -1. | ||
*/ | ||
public static double radiceTotale(double x) { | ||
|
||
// Sapreste dare una implementazione basata sul metodo di Newton? | ||
// https://en.wikipedia.org/wiki/Newton's_method | ||
// Può essere d'aiuto la nota https://math.mit.edu/~stevenj/18.335/newton-sqrt.pdf | ||
|
||
return x; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
Copyright 2024 Massimo Santini | ||
This file is part of "Programmazione 2 @ UniMI" teaching material. | ||
This is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This material is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this file. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package it.unimi.di.prog2.h06; | ||
|
||
import java.util.Scanner; | ||
|
||
/** Classe client per il calcolo delle radici quadrate. */ | ||
public class RadiciClient { | ||
|
||
/** . */ | ||
private RadiciClient() {} | ||
|
||
/** | ||
* Metodo che esercita il metodo di calcolo delle radici. | ||
* | ||
* <p>Legge una sequenza di {@code double} dal flusso di ingresso e ne emette la radice quadrata | ||
* nel flusso d'uscita. | ||
* | ||
* @param args non usato. | ||
*/ | ||
public static void main(String[] args) { | ||
try (Scanner s = new Scanner(System.in)) { | ||
while (s.hasNextDouble()) { | ||
double x = s.nextDouble(); | ||
double y = Radici.radiceParziale(x); | ||
System.out.println(Math.abs(y * y - x) < 0.001); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* Codice relativo alla lezione 06, per maggiori dettagli si veda il <a | ||
* href="https://prog2.di.unimi.it/diario">diario del corso</a>. | ||
*/ | ||
package it.unimi.di.prog2.h06; |
100 changes: 100 additions & 0 deletions
100
tests/it/unimi/di/prog2/h06/RadiciClient/expected-1.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
1 | ||
2 | ||
3 | ||
4 | ||
5 | ||
6 | ||
7 | ||
8 | ||
9 | ||
10 | ||
11 | ||
12 | ||
13 | ||
14 | ||
15 | ||
16 | ||
17 | ||
18 | ||
19 | ||
20 | ||
21 | ||
22 | ||
23 | ||
24 | ||
25 | ||
26 | ||
27 | ||
28 | ||
29 | ||
30 | ||
31 | ||
32 | ||
33 | ||
34 | ||
35 | ||
36 | ||
37 | ||
38 | ||
39 | ||
40 | ||
41 | ||
42 | ||
43 | ||
44 | ||
45 | ||
46 | ||
47 | ||
48 | ||
49 | ||
50 | ||
51 | ||
52 | ||
53 | ||
54 | ||
55 | ||
56 | ||
57 | ||
58 | ||
59 | ||
60 | ||
61 | ||
62 | ||
63 | ||
64 | ||
65 | ||
66 | ||
67 | ||
68 | ||
69 | ||
70 | ||
71 | ||
72 | ||
73 | ||
74 | ||
75 | ||
76 | ||
77 | ||
78 | ||
79 | ||
80 | ||
81 | ||
82 | ||
83 | ||
84 | ||
85 | ||
86 | ||
87 | ||
88 | ||
89 | ||
90 | ||
91 | ||
92 | ||
93 | ||
94 | ||
95 | ||
96 | ||
97 | ||
98 | ||
99 | ||
100 |