Skip to content
This repository has been archived by the owner on Mar 19, 2018. It is now read-only.

Your functions are backwards... #1

Closed
abferm opened this issue May 29, 2015 · 4 comments
Closed

Your functions are backwards... #1

abferm opened this issue May 29, 2015 · 4 comments

Comments

@abferm
Copy link

abferm commented May 29, 2015

Well, binary coded decimals are by nature higher than their integer counterparts... If you swap the names on your functions you will be good to go according to my math.

@dafyddcrosby
Copy link
Owner

@abferm First, thanks for taking a look! I'd forgotten about this module :-)

These functions implement 'natural' binary-coded decimal: http://en.wikipedia.org/wiki/Binary-coded_decimal#Basics

So the functions handle the encoding/decoding per-nibble, and the BCD should be treated as a string (not obvious from the source - I hadn't really expected reuse of this module ;-) and you need to take a look at the binary to see what's going on.

So for the doctest:

>>> int_to_bcd(34)
22

The integer

00100010
87654321
bit 2 and bit 6 are 1, which is 2 + 32 = 34

Broken down into nibbles for BCD

0010 0010
Which, referencing the table in Wikipedia, 0020 is 2. "2" + "2" = "22"

When you see the BCD as a string, int 34 -> bcd "22". I'll add some documentation so that this behaviour is a little more obvious. Again, thanks for taking a look!

@abferm
Copy link
Author

abferm commented Jun 1, 2015

I think I was misinterpreting what your methods were trying to do. From my understanding int_to_bcd would return the bcd equivalent of the provided integer, not decode an integer as BCD. In other words I expected int_to_bcd(22) to return binary 0010 0010 which is integer 34.

----- Original Message -----
From: "David T. Crosby" notifications@github.com
Date: Sunday, May 31, 2015 10:17 am
Subject: Re: [python-bcd] Your functions are backwards... (#1)
To: dafyddcrosby/python-bcd python-bcd@noreply.github.com
Cc: abferm abferm@wichita.edu

@abferm First, thanks for taking a look! I'd forgotten about this module :-)

These functions implement 'natural' binary-coded decimal: http://en.wikipedia.org/wiki/Binary-coded_decimal#Basics

So the functions handle the encoding/decoding per-nibble, and the BCD should be treated as a string (not obvious from the source - I hadn't really expected reuse of this module ;-) and you need to take a look at the binary to see what's going on.

So for the doctest:

int_to_bcd(34) > 22 >
The integer
00100010 > 87654321 > bit 2 and bit 6 are 1, which is 2 + 32 = 34 >
Broken down into nibbles for BCD
0010 0010 > Which, referencing the table in Wikipedia, 0020 is 2. "2" + "2" = "22" >
When you see the BCD as a string, int 34 -> bcd "22". I'll add some documentation so that this behaviour is a little more obvious. Again, thanks for taking a look!

Reply to this email directly or view it on GitHub.
-

@dafyddcrosby
Copy link
Owner

Ah, that makes sense. How do additional int_to_binary_bcd and binary_bcd_to_int functions sound? Clearer input/outputs, I think...

@abferm
Copy link
Author

abferm commented Jun 1, 2015

Possibly, I'm surprised that yours is the only BCD library I could find without a whole lot of searching. Would you consider adding an init.py file to make it a true module?

----- Original Message -----
From: "David T. Crosby" notifications@github.com
Date: Monday, June 1, 2015 11:25 am
Subject: Re: [python-bcd] Your functions are backwards... (#1)
To: dafyddcrosby/python-bcd python-bcd@noreply.github.com
Cc: abferm abferm@wichita.edu

Ah, that makes sense. How do additional int_to_binary_bcd and binary_bcd_to_int functions sound? Clearer input/outputs, I think...

Reply to this email directly or view it on GitHub.
-

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants