Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Converting Binary numbers to Decimal using decimal numbers
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
BigBear




PostPosted: Mon Apr 06, 2009 4:41 pm   Post subject: Converting Binary numbers to Decimal using decimal numbers

Turing:
fcn tobinary (num1 : string) : real
    %Converts a decimal number to a binary number
    var whole : string := ""
    var remainder : string := ""
    var Whole : int
    var Remainder : real
    var position : int
    for i : 1 .. length (num1)
        if num1 (i) = "." then
            position := i
        end if
    end for
    for i : 1 .. length (num1)
        if i < position then
            whole += num1 (i)
        elsif i >= position then
            remainder += num1 (i)
        end if
    end for

    Whole := strint (whole)
    Remainder := strreal (remainder)






    var temp : int
    var binary : string
    var num := Whole
    var intPower : int := 0
    var decimal : int := 0
    loop
        temp := num mod 2
        if temp = 1 then
            decimal := decimal + (10 ** intPower)
        end if
        intPower := intPower + 1
        num := num div 2
        exit when num = 0
    end loop
    binary := intstr (decimal)
    if Whole < 0 then
        binary := "-" + binary
    end if












    var decimalremainder : real := Remainder
    var decimalremainder1 : real
    var tempdecimal : string := ""
    var check : string
    var count : int := 0
    var del : real
    var remainder1 : string := "0"
    loop
        decimalremainder1 := decimalremainder * 2
        if decimalremainder1 >= 1 then
            tempdecimal += "1"
        else
            tempdecimal += "0"
        end if

        check := realstr (decimalremainder1, 1)
        for i : 1 .. length (check)
            if num1 (i) = "." then
                position := i
            end if
        end for
        for i : position .. length (check)
            remainder1 := check (i)
        end for
        del := strreal (remainder1)
        count += 1
        exit when del <= 0.10 or count = 14
    end loop

    var reorder : string := "."

    for decreasing i : length (tempdecimal) .. 1
        reorder += tempdecimal (i)
    end for

    var finally := strreal (binary) + strreal (reorder)

    result finally
end tobinary


put tobinary ("5.5")




I am trying to convert a decimal number with a decimal portion into a binary number with a decimal portion. I have added a lot of variables to try to break it down and get it to work. I have a seperate function for binary to decimal but the decimal portion is different.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Mon Apr 06, 2009 4:48 pm   Post subject: RE:Converting Binary numbers to Decimal using decimal numbers

This doesn't look like IEEE 754 Razz
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
BigBear




PostPosted: Mon Apr 06, 2009 5:02 pm   Post subject: RE:Converting Binary numbers to Decimal using decimal numbers

No it doesn't.....
Tony




PostPosted: Mon Apr 06, 2009 5:08 pm   Post subject: RE:Converting Binary numbers to Decimal using decimal numbers

So what kind of format are you going for here?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
BigBear




PostPosted: Mon Apr 06, 2009 5:09 pm   Post subject: RE:Converting Binary numbers to Decimal using decimal numbers

What do you mean by format?
Tony




PostPosted: Mon Apr 06, 2009 5:12 pm   Post subject: RE:Converting Binary numbers to Decimal using decimal numbers

How do you want the output to look? What is 5.5 supposed to look like, in binary?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
BigBear




PostPosted: Mon Apr 06, 2009 5:22 pm   Post subject: RE:Converting Binary numbers to Decimal using decimal numbers

I guess the 5.5 is right. But 5.4 doesn't work and it should be

101.01100110011001

Because exit when count = 14 but that could be changed to four or something.
Tony




PostPosted: Mon Apr 06, 2009 5:25 pm   Post subject: RE:Converting Binary numbers to Decimal using decimal numbers

ok, I see the 5. => 101.

but how are you going from .4 to .01100110011001 ?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
BigBear




PostPosted: Mon Apr 06, 2009 5:32 pm   Post subject: RE:Converting Binary numbers to Decimal using decimal numbers

you take the decimal portion of the number .4 and times it by 2 and the whole number portion is either a 1 or 0 so
Turing:
decimalremainder1 := decimalremainder * 2
        if decimalremainder1 >= 1 then
            tempdecimal += "1"
        else
            tempdecimal += "0"
        end if

then it checks the decimal portion of that new number and sees if it is less than .10 which it will not be because it has a repeating decimal portion of 0110

So it will iterate 14 times (as it sits) so the wholenumber portion will be added to

tempdecimal then it will be added to the 101 part
Tony




PostPosted: Mon Apr 06, 2009 5:49 pm   Post subject: RE:Converting Binary numbers to Decimal using decimal numbers

so, if I understand...

0.4 * 2 = 0.8 -- record 0
0.8 * 2 = 1.6 -- record decimal 1 as binary 1 ?

So the natural question then... how do you convert back to decimal? Razz
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
BigBear




PostPosted: Mon Apr 06, 2009 6:00 pm   Post subject: RE:Converting Binary numbers to Decimal using decimal numbers

But this function doesn't work

For decimal you can find how long the decimal portion is and start at a - number and do the div and mod way
[Gandalf]




PostPosted: Mon Apr 06, 2009 6:39 pm   Post subject: RE:Converting Binary numbers to Decimal using decimal numbers

wtd's Number Bases Article on the CompSci.ca Wiki.
BigBear




PostPosted: Mon Apr 06, 2009 6:48 pm   Post subject: Re: RE:Converting Binary numbers to Decimal using decimal numbers

[quote="[Gandalf] @ Mon Apr 06, 2009 6:39 pm"]wtd's Number Bases Article on the CompSci.ca Wiki.[/quote]

Yeah I understand the number systems but I am trying to find something wrong with my function I thibk it is a little thing
Insectoid




PostPosted: Mon Apr 06, 2009 7:36 pm   Post subject: RE:Converting Binary numbers to Decimal using decimal numbers

That's the way I use, and the way Dan was taught in 2nd year uni. I still haven't figured out how to convert back, as you are discarding numbers.
BigBear




PostPosted: Mon Apr 06, 2009 7:46 pm   Post subject: Re: RE:Converting Binary numbers to Decimal using decimal numbers

insectoid @ Mon Apr 06, 2009 7:36 pm wrote:
That's the way I use, and the way Dan was taught in 2nd year uni. I still haven't figured out how to convert back, as you are discarding numbers.


I have finished the function to convert from binary to decimal if that's what you meant by convert back
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 15 Posts ]
Jump to:   


Style:  
Search: