|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Registered User
Iscritto dal: Nov 2012
Messaggi: 6
|
[C] Calcolo equazione quadratica
Codice:
#include <stdio.h>
float absoluteValue (float x) {
if (x < 0)
x = -x;
return x;
}
float squareRoot (float x) {
const float limit = 0.0001;
float guess = 1;
while (absoluteValue (guess * guess - x) >= limit)
guess = (x / guess + guess) / 2;
return guess;
}
int main (void) {
int a;
int b;
int c;
float det;
float root1;
float root2;
printf ("Inserisci variabile intera a: ");
scanf ("%i", &a);
printf ("Inserisci variabile intera b: ");
scanf ("%i", &b);
printf ("Inserisci variabile intera c: ");
scanf ("%i", &c);
det = (b * b) - (4 * (a * c));
root1 = ( -b + squareRoot (det)) / (2 * a);
root2 = ( -b - squareRoot (det)) / (2 * a);
if (det < 0)
printf ("Le radici sono immaginarie");
else
printf ("x1 = %f | x2 = %f \n", root1, root2);
return 0;
}
|
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Jul 2003
Città: Alessandria
Messaggi: 10167
|
L'ho provato. In realtà credo funzioni, l'ho debuggato un po' e ho notato che non riesco ad uscire da questo while:
Codice:
float squareRoot (float x) {
const float limit = 0.0001;
float guess = 1;
while (absoluteValue (guess * guess - x) >= limit)
guess = (x / guess + guess) / 2;
return guess;
}
Cerca di capire tu perchè... Io ti ho detto dove sbaglia
__________________
Dell XPS 13 (9350) :: i5-2500K - HD6870 - AsRock Z68 Pro3 - Corsair Vengeance 8GB (4x2) DDR3 :: Samsung Galaxy S4 GT-i9505
|
|
|
|
|
|
#3 | |
|
Senior Member
Iscritto dal: Nov 2005
Città: Texas
Messaggi: 1722
|
Quote:
Prova a stampare ogni passo del ciclo
__________________
In God we trust; all others bring data |
|
|
|
|
|
|
#4 |
|
Registered User
Iscritto dal: Nov 2012
Messaggi: 6
|
Grazie mille a tutti e due, ho modificato un po' e risolto il problema
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 03:23.




















