Ternary nesting -- bad idea?

This is a discussion on Ternary nesting -- bad idea? within the C++ Programming forums, part of the General Programming Boards category; This functions returns the n-th number in the fibonacci sequence. Besides crappy readability, is there any reason for not doing ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917

    Ternary nesting -- bad idea?

    This functions returns the n-th number in the fibonacci sequence. Besides crappy readability, is there any reason for not doing it this way?
    Code:
    long unsigned int fibby(int n, long unsigned int num1 = 0LU, long unsigned int num2 = 1LU, int count = 1);
    {
       return (!n || n == 1) ? 0 : ((count < n) ? fibby(n, num2, num2 + num1, count + 1) : num2);
    }
    I've learned that the ternary can be very optimal, but does using it this way backfire that optimization? I don't think so, but tell we what you know. Just a thought.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    whoops -- it's (n - 1):
    Code:
    long unsigned int fibby(int n, long unsigned int num1 = 0LU, long unsigned int num2 = 1LU, int count = 1);
    {
       return (!n || n == 1) ? 0 : ((count < n - 1) ? fibby(n, num2, num2 + num1, count + 1) : num2);
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    looks like crap
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    looks like crap
    lol

    Try this, although this is more of a hack.

    Code:
    /*
    
       1/sqrt(5)  *  [1/2(1+sqrt(5)] ^n
       
       Approximates the nth term of the fibby sequence
       Works well when n >= 5
    */
    
    #include<iostream>
    #include<math.h>
    #include<iomanip>
    
    int fibby(int);
    
    using namespace std;
    
    int main()
    {
        int enter;
        cout<<"Enter the nth term for the fibby sequence.";
        cout<<"Works well when n>=5:";
        cin>>enter;
        
        //call the function fibby pass in the variable 'enter'
        fibby(enter);
        
        int stop;
        cin>>stop;
    }    
    
    
    
    //function declaration
    int fibby(int n)
    {
        float partone;
        float parttwo;
        float partthree;
        float answer;
        
        partone=1/sqrt(5.0);
        
        parttwo=0.5*(1+sqrt(5.0));
        
        partthree=pow(parttwo,n);
        answer=(partone*partthree);
        int var;
        if(answer<10)
        {
            var=1;
        }
        if((answer>=10)&&(answer<=100))
        {
            var=2;
        } 
        if  ((answer>=100)&&(answer<=1000))
        {     
            var=3;
        } 
          if  ((answer>=1000)&&(answer<=10000))
        {     
            var=4;
        } 
        if  ((answer>=10000)&&(answer<=100000))
        {     
            var=5;
        } 
        
        cout<< setprecision(var)<<answer;
    }

    You can condense the set precision bit but I couldn't be bothered.


  5. #5
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    how about this:

    Code:
    unsigned long fib(int n, unsigned long current = 1, unsigned long prev = 0)
    {
        return(n <= 1 ? current : fib(n - 1, prev + current, current));
    }
    
    int main()
    {
        for(int i = 1; i <= 20; i++)
        {
            std::cout << fib(i) << std::endl;
        }
        return(0);
    }
    I realise it doesn't answer your question, but I had fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An idea that's out of my reach of knowledge
    By Akkernight in forum Tech Board
    Replies: 12
    Last Post: 04-08-2009, 10:35 PM
  2. Bad File number?
    By Unregistered in forum Linux Programming
    Replies: 4
    Last Post: 05-31-2002, 09:55 AM
  3. Global variables? Bad! Yes, but to what extent?
    By Boksha in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2002, 05:37 PM
  4. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21