Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Hi ich versuche mich jetzt schon seit Stunden aber ich schaffe es nicht.

Die Aufgabe: Die 200ste Fibonacci Zahl (10 Stellen) herrausfinden. Mein Programm liefert mir aber ab der 185zigsten nur noch 1.#INF.

Wie umgehe ich das Zahlenlimit von float? Liegt es überhaupt daran?

Hier mein Code:

#include <iostream>

using namespace std;

int main (void)

{

float a = 2;

float b = 1;

float old_a = 0;

int counter = 0;

while (counter < 200)

{

cout << "Counter: " << counter+1 << "\t";

cout.precision(10);

cout << a/b << endl;

old_a = a;

a = a + b;

b = old_a;

counter++;

}

return(0);

}

warum überhaupt float?

Die 200. Fibonacci-Zahl ist ungefähr 2.8 * 10^41. Der Wertebereich eines 32-Bit-float geht nur bis 3.4 * 10^38. Außerdem hat so ein float ohnehin nur 7 signifikante Stellen, so dass du damit gar nicht 10 Stellen Genauigkeit erreichen kannst.

Versuch's mal mit double.

An double habe ich garnicht gedacht, dachte das wäre eine Datentyp für Festkomma Zahlen und nicht für Gleitkommazahlen :-)

Jetzt klappt alles super!!!!!!

Vielen Dank!!

warum überhaupt float?

Frag ich mich auch. Mit Double gehts sowohl rekursiv, als auch iterativ:


    double fibIter(int num)

    {

      double result=1;

      double a=1;

      double b=1;


      for(int i=3; i<=num; ++i)

      {

          result=a+b;

          a=b;

          b=result;

      }

      return result;

    }


    double fibRec(int num)

    {

      if(num<=2)

          return 1;

      return fibRec(num-1) + fibRec(num-2);

    }

Bearbeitet von TDM

Da brauchst du aber schon einen 128-Bit-Integer, wenn du die 200. Fibonacci-Zahl unterbringen willst.

Ja, hatte ich mir auch grad überlegt, siehe Edit - double ist schon besser. ;)

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.