Generic C Standard Library Functions

  • qsort– I can sort an array of any type! To do that, I need you to provide me a function that can compare two elements of the kind you are asking me to sort.
  • bsearch– I can use binary search to search for a key in an array of any type! To do that, I need you to provide me a function that can compare two elements of the kind you are asking me to search.
  • lfind– I can use linear search to search for a key in an array of any type! To do that, I need you to provide me a function that can compare two elements of the kind you are asking me to search.
  • lsearch- I can use linear search to search for a key in an array of any type! I will also add the key for you if I can’t find it. In order to do that, I need you to provide me a function that can compare two elements of the kind you are asking me to search.
  • scandir– I can create a directory listing with any order and contents! To do that, I need you to provide me a function that tells me whether or not you want me to include a given directory entry in the listing. I also need you to provide me a function that tells me the correct ordering of two given directory entries.

Floating Point

  • Fixed Point Representation: A method to represent real numbers by adding binary decimal places. It has limitations in the range of numbers it can represent.  
  • Floating Point Representation: A more flexible method to represent a wider range of real numbers, similar to scientific notation.  
  • IEEE Floating Point Standard: The most common standard for representing floating-point numbers.  
  • Floating Point Imprecision: Floating point numbers may introduce approximation and rounding errors.  
  • Floating Point Arithmetic: Arithmetic operations on floating point numbers can have unexpected behavior due to precision limits (e.g., non-associativity).  
#include <stdio.h>

int main() {
    float a = 0.1;
    float b = 0.2;
    if (a + b == 0.3) {
        printf("Equal\n");
    } else {
        printf("Not Equal\n"); // This will print
    }
    printf("%.20f\n", a + b); // Output: 0.30000000000000004
    return 0;
}

Conceptual - actual IEEE representation is more complex

   Sign  | Exponent | Mantissa
   1 bit |  8 bits  | 23 bits

Floating Point in C

  • C Guarantees Two Levels– float single precision– double double precision
  • Conversions/Casting– Casting between int, float, and double changes bit representation– double/float → int
    • Truncates fractional part
    • Like rounding toward zero
    • Not defined when out of range or NaN: Generally sets to TMin
  • int → double
    • Exact conversion, as long as int has ≤ 53 bit word size
  • int → float
    • Will round according to rounding mode

Reading

What Every Computer Scientist Should Know About Floating-Point Arithmetic

IEEE-754 Floating Point Converter

Floating Point Grid

Float Toy

Binary / Decimal Converter

About numbers, and how to get subtraction wrong

Fast Inverse Square Root — A Quake III Algorithm