Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // lab2_8
- //
- // Created by Денис Домашевич on 23.02.2018.
- // Copyright © 2018 Денис Домашевич. All rights reserved.
- //
- #include <math.h>
- #include <stdio.h>
- #include <stdbool.h>
- //Проверка на ввод числа тип LongLong
- long long readLongLong() {
- long long res;
- while(true) {
- if(scanf("%lld", &res) && getchar() == '\n' && res >= 0) {
- return res;
- } else {
- printf("Try again\n");
- while(getchar() != '\n') {}
- }
- }
- }
- //Проверка на ввод числа типа Double
- double readDouble() {
- double res;
- while(true) {
- if(scanf("%lf", &res) && getchar() == '\n') {
- return res;
- } else {
- printf("Try again\n");
- while(getchar() != '\n') {}
- }
- }
- }
- void solve(long long x, double eps, double currY) {
- if(x == 0) {
- printf("Result of my function %d\n", 0);
- printf("Number of iterations %d\n", 0);
- printf("Result of standart function %lf\n", sqrt((double)x));
- return ;
- }
- long long iter = 0;
- while(true) {
- iter++; // Подсчёт количества итераций
- double newY = (currY + (x / currY)) / 2;
- if(eps + newY >= currY) {
- break;
- }
- currY = newY;
- }
- printf("Result of my function %lf\n", currY);
- printf("Number of iterations %lld\n", iter);
- printf("Result of standart function %lf\n", sqrt((double)x));
- }
- int main() {
- printf("Enter x = \n");
- long long x = readLongLong();
- printf("Enter eps = \n");
- double eps = readDouble(), Y = (double)x;
- solve(x, eps, Y);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement