# Fibonacci Formula, How to round in C++?

This is a discussion on Fibonacci Formula, How to round in C++? within the C++ Programming forums, part of the General Programming Boards category; Ok, my question is simple, how do you round in c++? im sure there is some prebuilt function to do ...

1. ## Fibonacci Formula, How to round in C++?

Ok, my question is simple, how do you round in c++? im sure there is some prebuilt function to do this, least i hope.

What im trying to do is build a test for figuring out a simple Fibonacci formula, (n-1+n-2)=x type thing. Basically you can do it perfectly through Recursion, but as all you know far better than me (assuming i got this right), with big numbers you end up having thousands of instances of the function to complete your Fabonacci formula.

What i wanna do is figure the percentage of a number and round it to zero, i wanna see where (61%of n+40% of n)=x starts going wrong. Cuz rounded to zero, in small numbers that works just as good as recursion, im just curious as to what number it will go off on. Thus i need to know how to round in C++.

Thanks
(if you dont know what Fabonacci is dont worry bout it, all i need to know is how to round in c++ lol).

Also, sidenote, here is an example of a Fab Formula.
1,1,2,3,5,8,13,21,34,55,89,144,223..

2. There is a formula for calculating fibonacci numbers without recursion or loops.
The n:th number equals
1/s * ( ((1+s)/2)^n - ((1-s)/2)^n )
where
s = sqrt(5)

The proof involves setting up an equation with matrices and calculating A^n for a 2x2 matrix.

EDIT: even though it contains a number of sqrt(5), the result is always an integer.

3. ok, thanks lol.
What about round though? still curious

4. one way is to use int variables and add 0.5 to the number you want to round. ints ignore any decimal places (truncates, whatever). Therefore if the given value is less than 0.5 it will round down to the current value and if 0.5 or above it will round up to the next int value.

5. oh duh, forgot about int trunacating decimals. So 1547.2445213 it would round to 1547 right?

6. Yes.

1547.2445213 -> 1547
1547.4999999 -> 1547
1547.5000000 -> 1548
1547.9999999 -> 1548

7. In <cmath>, you can also use floor() and ceil() to preserve the floating point data type (which is a good thing with really large numbers).

Sang drax, the proof I've seen of that formula does not make use of matrices (I can't remember it right off, but I could probably figure it out after a while). I'd be interested in seeing that one if you have a link handy.

8. 1547.9999999 -> 1548
WRONG!
Casting a double to an int (either explict or just through assignment) TRUNCATES!

Code:
```double x = 1547.999999;
int y = x;
int z = int(x);```
Both y and z with be 1547 not 1548

9. jlou was just trying to say that
1547.9999999 should be rounded up to 1548,
I believe.

10. Originally Posted by Zeusbwr
So 1547.2445213 it would round to 1547 right?
Originally Posted by jlou
Yes.

1547.2445213 -> 1547
1547.4999999 -> 1547
1547.5000000 -> 1548
1547.9999999 -> 1548
Originally Posted by Thantos
WRONG!
Casting a double to an int (either explict or just through assignment) TRUNCATES!

Code:
```double x = 1547.999999;
int y = x;
int z = int(x);```
Both y and z with be 1547 not 1548
Originally Posted by alphaoide
jlou was just trying to say that
1547.9999999 should be rounded up to 1548,
I believe.
Yes. I was answering Zeusbwr's question, and providing extra examples.

11. Originally Posted by Zach L.
Sang drax, the proof I've seen of that formula does not make use of matrices (I can't remember it right off, but I could probably figure it out after a while). I'd be interested in seeing that one if you have a link handy.
I don't have a link to it on the Internet, but I can try to show you here on the internet:
The idea is do calculate a power of a 2x2 matrix by diagonalizing it.
Code:
```F[k] is the k:th Fibonacci number

X[k] is a 2x1 matrix:
(F[k]    )
(F[k-1] )  = X[k]

A is a matrix such that
X[k] = A*X[k-1]

A then equals
( 1 1 )
( 1 0 )

The n:th Fibonacci number can then be written
X[n+1] = A^n * X[1]
We know that X[1] equals
( 1 )
( 0 )
so all we have to do is calculating A^n and multiplying it with X[1].

This is done by calculating the two eigenvalues of A which are:
(1 + sqrt(5))/2
(1 - sqrt(5))/2
using the eigenvalues and eigenvectors we can diagonalize A by writing A as S*D*S^-1
where S is a matrix with the eigenvectors and D is a diagonal matris.
A^n  = S * D^n * S^-1

This equals, after the three matrix multiplications, the formula I posted above.```
This will make no sense if you haven't studied linear algebra.