Modelib: Fichier source de Fraction.cpp

Page principale | Liste des namespaces | Hiérarchie des classes | Liste alphabétique | Liste des classes | Liste des fichiers | Membres de namespace | Membres de classe | Membres de fichier

# Fraction.cpp

Aller à la documentation de ce fichier.

    00001 /*
    00002     Copyright (c) 2005, Quentin Lequy and Romain Gaucher
    00003     All rights reserved.
    00004
    00005     Redistribution and use in source and binary forms, with or
    00006     without modification, are permitted provided that the following
    00007     conditions are met:
    00008
    00009         * Redistributions of source code must retain the above copyright
    00010           notice, this list of conditions and the following disclaimer.
    00011
    00012         * Redistributions in binary form must reproduce the above copyright
    00013           notice, this list of conditions and the following disclaimer in the
    00014           documentation and/or other materials provided with the distribution.
    00015
    00016         * Neither the name of Quentin Lequy or Romain Gaucher nor the names
    00017           of its contributors may be used to endorse or promote products
    00018           derived from this software without specific prior written permission.
    00019
    00020     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    00021     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    00022     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    00023     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    00024     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    00025     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    00026     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
    00027     AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00028     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    00029     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00030 */
    00031
    00032 #include "Fraction.h"
    00033 #include <map>
    00034 #include <iostream>
    00035
    00036 namespace Modelib {
    00037
    00038 using namespace std;
    00039
    00044 FractionConvert::FractionConvert(unsigned _n)
    00045 {
    00046  map<float,Fraction> tmp;
    00047  map<float,Fraction>::iterator trouve;
    00048  for(unsigned i = 0; i <_n; ++i)
    00049  {
    00050    for(unsigned j = 0; j <i; ++j)
    00051    {
    00052     trouve = tmp.find(float(j)/float(i));
    00053     if( trouve == tmp.end() )
    00054       tmp[float(j)/float(i)] = Fraction(j,i);
    00055     else if( trouve->second.num > i )
    00056       trouve->second = Fraction(j,i);
    00057    }
    00058  }
    00059  nfrac = tmp.size();
    00060  frac = new Fraction[nfrac];
    00061  reel = new float[nfrac];
    00062
    00063  unsigned i =0;
    00064  for(trouve = tmp.begin(); trouve != tmp.end(); ++trouve)
    00065  {
    00066    reel[i] = trouve->first;
    00067    frac[i] = trouve->second;
    00068    ++i;
    00069  }
    00070 }
    00071
    00075 FractionConvert::~FractionConvert()
    00076 {
    00077   delete [] frac;
    00078   delete [] reel;
    00079 }
    00080
    00081
    00090 bool FractionConvert::Approx(float nb, int & num, unsigned  & den) const
    00091 {
    00092    unsigned a = 0,b=nfrac-1;
    00093    if(nb==0.0)
    00094    {
    00095        num = 0;
    00096        den = 1;
    00097        return false;
    00098    }
    00099
    00100    while(a<b)
    00101    {
    00102      unsigned mil = (a+b)/2;
    00103      if(reel[mil] < nb)
    00104         a = mil + 1;
    00105      else
    00106         b = mil;
    00107    }
    00108    if(a < nfrac)
    00109    {
    00110      if( reel[a]-nb >= nb - reel[a-1] )
    00111        a--;
    00112      num = frac[a].num;
    00113      den = frac[a].den;
    00114      return true;
    00115    }
    00116    else
    00117      return false;
    00118 }
    00119
    00120 }