<commit_before>//
//  Double_AsFraction.swift
//  Double_AsFraction
//
//  Created by <NAME> on 8/21/16.
//  Copyright © 2016 Koteray. All rights reserved.
//

import Foundation

extension Double {
    
    enum RoundTechnique {
        case Round
        case Up
        case Down
    }
    
    /// The positive number to the left of the decimal point
    var number: Double {
        get {
            return floor(fabs(self))
        }
    }
    
    /// The positive number to the right of the decimal point
    var decimal: Double {
        get {
            return fabs(self) - number
        }
    }

    /**
     
     Fractions must be a sorted array of fraction - sort by the value.
     
     
     */
    
    func asFraction(fractions: [Fraction],roundTechnique: RoundTechnique) -> String {
        
        let bestGuess = Int(floor(decimal * Double(fractions.count)))
        
        return findNearest(bestGuess, fractions: fractions, roundTechnique: roundTechnique)
        
    }
    
    private func findNearest(bestGuess: Int, fractions: [Fraction], roundTechnique: RoundTechnique) -> String {
        
        switch true {<commit_msg>Refactored and commented the rounding logic enum<commit_after>//
//  Double_AsFraction.swift
//  Double_AsFraction
//
//  Created by <NAME> on 8/21/16.
//  Copyright © 2016 Koteray. All rights reserved.
//

import Foundation

extension Double {
    
    /**
     Logic to round values
     
      * Round: Follows Apple's logic: When equidistant, round away from zero
      * Up: Towards positive infinity
      * Down: Towards negative infinity
    */
    enum RoundLogic {
        case Round
        case Up
        case Down
    }
    
    /// The positive number to the left of the decimal point
    var number: Double {
        get {
            return floor(fabs(self))
        }
    }
    
    /// The positive number to the right of the decimal point
    var decimal: Double {
        get {
            return fabs(self) - number
        }
    }

    /**
     
     Fractions must be a sorted array of fraction - sort by the value.
     
     
     */
    
    func asFraction(fractions: [Fraction],roundTechnique: RoundLogic) -> String {
        
        let bestGuess = Int(floor(decimal * Double(fractions.count)))
        
        return findNearest(bestGuess, fractions: fractions, roundTechnique: roundTechnique)
        
    }
    
    private func findNearest(bestGuess: Int, fractions: [Fraction], roundTechnique: RoundLogic) -> String {
        
        switch true {
{'fraction_non_alphanumeric': 0.08254716981132075, 'fraction_numerical': 0.007075471698113208, 'mean_word_length': 1.8499440089585666, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1775516', 'n_tokens_mistral': 757, 'n_tokens_neox': 713, 'n_words': 224}