# Measure class, precision, significant digits, and divmod()

Discussion in 'Python' started by Ethan Furman, Jul 15, 2008.

1. ### Ethan FurmanGuest

Hey all.

My thanks to all who have responded so far with my other questions. It
is much appreciated.

Some background on what I'm doing (a good explanation can be found at
http://www.hazelwood.k12.mo.us/~grichert/sciweb/phys8.htm): When
measuring, there is some uncertainty as to the *exact* value; so, when
doing calculations with these measured numbers, one should not present
an answer that seems more accurate than the source. For addition and
subtraction this means the final answer cannot be more precise than the
least precise input: 5.0 + 21.23 = 26.2 (first number only measured to
the tenths, so answer only good to the tenths); for multiplication and
division this means the answer cannot have more significant digits than
the input with the fewest significant digits: 231 * 0.25 = 58 (0.25
only has two significant digits, so answer only has two significant
digits). Important point -- counting is not subject to precision error.

As I have mentioned before, I am making this Measure class for two
reasons: experience with unit testing, I like playing with numbers, I
am unaware of anything like this having yet been done (okay, three
reasons .

So far, I have the init, add, sub, mul, div, cmp, neg, pos, and abs
done, and I'm ready to tackle floordiv, mod, and divmod... okay, maybe
'ready' is too strong a word -- one of the obstacle's I'm facing is that
I, myself, don't have any real use for this class, so I'm stuck on
deciding how floordiv, mod, and divmod should function. As mentioned
above, counting is precise... so should divmod (a // b, a % b), which
returns the count of times b goes into a, and the remainder, be an exact
number, or should it behave like floor(a / b), and preserve the inherent
inaccury present in the measured value a? Should I have floordiv
preserve inaccuracy, and divmod be exact?

Any and all feedback welcome, particularly from anyone who might
actually use the Measure class.

~Ethan

Ethan Furman, Jul 15, 2008

2. ### Ken StarksGuest

Ethan Furman wrote:
> Hey all.
>

<snip>
>
> As I have mentioned before, I am making this Measure class for two
> reasons: experience with unit testing, I like playing with numbers, I
> am unaware of anything like this having yet been done (okay, three
> reasons .
>

<snip>
>
> Any and all feedback welcome, particularly from anyone who might
> actually use the Measure class.
>
> ~Ethan

I have been following this thread with interest, because i am doing
something similar, namely writing python to provide myself with
experience with unit tests and i like playing with numbers.

In my case the numbers are 'long rationals' (infinite precision)
and will include both floats, the decimal type, extended 'decimals'
-- i.e. possibly with a repeating expansion (or in any base) --
and finite continued fractions as derived classes.

I also hope to deal with repeating continued fractions later.

+++++++++++++++++++++++++++++++++++++++

My principal interest in your work, however, is in the use of
unit tests as a pedagogical method of teaching programming, whether
self-taught or taught by an instructor.

The students would be taught how to __run__ a unit test-suite
at as early an opportunity as possible. I can't see
why an eleven or twelve year old should not be able to cope
with it in hour one or two of a carefully structured course.

What is needed for such an approach is a copy--in a public
place--of your unit tests for all methods
that are really part of the funtionality; together with
blanked-out definitions for them (i.e just the first line,
any in-code documentation, and 'pass' ).

You would not include any 'internal' methods that are merely the way
__you__ happened to achieve the result.

The programming 'exercise' would then be for the students, or
groups of students, to roll their own version until their code
passed all the unit tests.

Ken Starks, Jul 15, 2008

3. ### Ethan FurmanGuest

Ken Starks wrote:
> Ethan Furman wrote:
>
>> Hey all.
>>

> <snip>
>
>>
>> As I have mentioned before, I am making this Measure class for two
>> reasons: experience with unit testing, I like playing with numbers, I
>> am unaware of anything like this having yet been done (okay, three
>> reasons .
>>

> <snip>
>
>>
>> Any and all feedback welcome, particularly from anyone who might
>> actually use the Measure class.
>>
>> ~Ethan

>

[snip]

> +++++++++++++++++++++++++++++++++++++++
>
> My principal interest in your work, however, is in the use of
> unit tests as a pedagogical method of teaching programming, whether
> self-taught or taught by an instructor.
>
> The students would be taught how to __run__ a unit test-suite
> at as early an opportunity as possible. I can't see
> why an eleven or twelve year old should not be able to cope
> with it in hour one or two of a carefully structured course.
>
> What is needed for such an approach is a copy--in a public
> place--of your unit tests for all methods
> that are really part of the funtionality; together with
> blanked-out definitions for them (i.e just the first line,
> any in-code documentation, and 'pass' ).
>
> You would not include any 'internal' methods that are merely the way
> __you__ happened to achieve the result.
>
> The programming 'exercise' would then be for the students, or
> groups of students, to roll their own version until their code
> passed all the unit tests.

Definitely an interesting idea. I'm not sure if I should be worried
about the 12-13 year old students tackling a complex class such as a new
number class, or if I should prepare to be embarrased by how much
simpler they are able to make equivalent code!

At any rate, once I'm done the code will be released under a very
friendly license, so you'll be free to do that should you desire to.

~Ethan

Ethan Furman, Jul 15, 2008
4. ### Ken StarksGuest

Eclipse, Pydev, question

I have a small project for further development
in eclipse, using the pyDev plug-in.

I am working on foo.py and bar.pyc is also
in the directory.

bar.py is not in the directory; it is someone
else's (confidential) file, and I don't get
the python source.

Can I run bar.pyc from eclipse ?

Ken Starks, Jul 16, 2008