Fixed Point Maths

version 5 by Michael Callaghan

  • Home page
  • Beginning
  • Previous



  • Section 2 - Conversion routine

    To decide which real number is the number derived from (T - indexed text):
        if T is "":
            now invalid conversion is true;
            decide on 0 as a fixed point number;
        replace the regular expression " " in T with "";
        let negative flag be false;
        let dec part be true;
        let result be a number;
        let first be a number;
        let second be a number;
        let second part test be true;
        let length be the number of characters in T;
        let multiplier be 1;
        repeat with N running from 0 to length minus 1:
            let pos be length minus N;
            let matched be false;
            let test char be character number pos in T;
            if test char is "-":
                unless pos is 1:
                    now invalid conversion is true;
                    decide on 0 as a fixed point number;
                let negative flag be true;
                let matched be true;
            if test char is "+":
                unless pos is 1:
                    now invalid conversion is true;
                    decide on 0 as a fixed point number;
                let matched be true;
            if test char is ".":
                let second be result;
                let second part test be false;
                if multiplier is less than divisor:
                    now invalid conversion is true;
                    decide on 0 as a fixed point number;
                let result be 0;
                let multiplier be 1;
                let matched be true;
            if test char is "0":
                let multiplier be multiplier times 10;
                let matched be true;
            if test char is "1":
                let result be multiplier plus result;
                let multiplier be multiplier times 10;
                let matched be true;
            if test char is "2":
                let result be 2 times multiplier plus result;
                let matched be true;
                let multiplier be multiplier times 10;
            if test char is "3":
                let result be 3 times multiplier plus result;
                let matched be true;
                let multiplier be multiplier times 10;
            if test char is "4":
                let result be 4 times multiplier plus result;
                let matched be true;
                let multiplier be multiplier times 10;
            if test char is "5":
                let result be 5 times multiplier plus result;
                let matched be true;
                let multiplier be multiplier times 10;
            if test char is "6":
                let result be 6 times multiplier plus result;
                let matched be true;
                let multiplier be multiplier times 10;
            if test char is "7":
                let result be 7 times multiplier plus result;
                let matched be true;
                let multiplier be multiplier times 10;
            if test char is "8":
                let result be 8 times multiplier plus result;
                let matched be true;
                let multiplier be multiplier times 10;
            if test char is "9":
                let result be 9 times multiplier plus result;
                let matched be true;
                let multiplier be multiplier times 10;
            if matched is false:
                now invalid conversion is true;
                decide on 0 as a fixed point number;
            if second part test is true:
                if multiplier is greater than divisor:
                    now invalid conversion is true;
                    decide on 0 as a fixed point number;
        let first be result;
        let final result be the real number with whole part first fractional part second;
        if negative flag is true:
            let final result be the negation of final result;
        now invalid conversion is false;
        decide on final result.

    Fixed Point Maths ends here.