Thread: double_to_fixed function and file IO

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    12

    double_to_fixed function and file IO

    Hello,

    Based on this thread :
    Read string convert and write

    I wrote the code below. It reads floating point numbers from a test file, converts them to a fixed point integer and writes them to another file.

    The code seems to work - but I'd like to get your feedback about it.
    Do you see any weak spots ? What would you change ? Style ? Anything...

    Code:
    #include <iostream>
    #include <fstream>
    #include <cmath>
    using namespace std ;
    
    /*
    This function converts a floating point number to a signed integer fixed point representation.
    Selecting whether to treat the input as signed or unsigned is done using the "sign" boolean.
    The integer bits "budget" can be configured using the number_integer_bits argument.
    The fraction bits "budget" can be configured using the number_fraction_bits argument.
    */
    
    unsigned int double_to_fixed ( bool sign , unsigned int number_integer_bits , unsigned int number_fraction_bits , double number )
    {
        double absolute_number = abs ( number ) ; // Get the absolute value for the number.
        double fraction_absolute_number = absolute_number - int ( absolute_number ) ; // Decrement the integer portion to be left only with the fraction.
        unsigned int accumulator = 0 ; // The accumulator will be used to calculate the integer portion.
        unsigned int result ; // The accumulator will be used to calculate the integer portion.
    
        for ( int index = number_integer_bits - 1 ; index >= 0 ; index -- )
        {
            if ( accumulator + pow ( 2 , index ) <= absolute_number ) // Checking if we should include the current index in the result.
            {
                accumulator = accumulator + pow ( 2 , index ) ;
            }
        }
    
        unsigned int scaled_fraction_absolute_number = int ( fraction_absolute_number * pow ( 2 , number_fraction_bits ) ) ;
        unsigned int scaled_integer_absolute_number = accumulator * pow ( 2 , number_fraction_bits ) ;
        unsigned int integer_plus_fraction = scaled_integer_absolute_number + scaled_fraction_absolute_number ;
        if ( sign == 1 ) // Treat the number as signed
        {
            unsigned int all_ones = pow ( 2 , 1 + number_integer_bits + number_fraction_bits ) - 1 ; // 1 + is for the extra sign bit.
            result = all_ones - integer_plus_fraction + 1 ; // 2's complement to get the negative value.
        }
        else // Treat the number as unsigned
        {
            result = integer_plus_fraction ;
        }
        // cout << int ( result ) << endl ; // For Debug purposes.
        return result ;
    }
    
    int main ( )
    {
        ifstream file_in ( "file_in.txt" ) ;
        ofstream file_out ( "file_out.txt" ) ;
        double number ;
    
        /*
            The function arguments:
            1 - Treat the input as signed.
            10 - The integer bits budget is 10 bits.
            16 - The fraction bits budget is 16 bits.
        */
    
        while ( file_in >> number )
        {
            file_out << double_to_fixed ( 1 , 10 , 16 , number ) << '\n' ;
        }
    
        return 0 ;
    }
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I prefer modf to num - int(num) as a means to getting just the fractional part.
    modf(3) - Linux man page

    Using pow(2,index) to calculate powers of 2 is clunky compared to 1<<index
    Also, the bit shift approach doesn't suffer from rounding errors, which I wouldn't entirely trust pow() to not have at some point.

    > unsigned int scaled_integer_absolute_number = accumulator * pow ( 2 , number_fraction_bits ) ;
    This would just be
    unsigned int scaled_integer_absolute_number = accumulator << number_fraction_bits;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2020
    Posts
    12
    Thanks Salem,

    Quote Originally Posted by Salem View Post
    I prefer modf to num - int(num) as a means to getting just the fractional part.
    modf(3) - Linux man page
    This is the line in question:
    Code:
    double fraction_absolute_number = absolute_number - int ( absolute_number ) ; // Decrement the integer portion to be left only with the fraction.
    How exactly do you suggest to re-writes it with modf() ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FILE * refrig (file pointer) to function
    By _jamie in forum C Programming
    Replies: 3
    Last Post: 01-04-2019, 09:56 PM
  2. Replies: 4
    Last Post: 08-25-2013, 05:09 AM
  3. Replies: 2
    Last Post: 05-10-2012, 05:59 PM
  4. fwrite function to write png file stream into a file
    By mdivya in forum C Programming
    Replies: 2
    Last Post: 08-26-2011, 12:10 AM
  5. Replies: 3
    Last Post: 11-21-2006, 07:26 PM

Tags for this Thread