Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-25 Thread spir

On 01/26/2014 02:12 AM, Steven D'Aprano wrote:

On Sat, Jan 25, 2014 at 09:11:56AM +0100, spir wrote:


As a foreigner, I noticed that english native speakers use both the series
round / square / curly / angle brackets, and individual terms parens (no
'd' ;-) / brackets / braces / chevrons. No major issue, except for
'brackets' which can be either a collective term or specific to [].


In the UK and Australia, "brackets" on its own refers to round brackets
(parentheses), as they are the most common form. We never use "brackets"
on its own to mean [], but only (), and the only time we bother to say
"round brackets" is when there is a need to disambiguate them from
square ones.


I learn english everyday ;-) thank you, Steven!

d

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread Keith Winston
On Sat, Jan 25, 2014 at 5:09 PM, Oscar Benjamin
 wrote:
> Perhaps it would be better though to point at this:
 round(D('0.123456'), 3)
> Decimal('0.123')

I think you are right. I didn't even think of round(). I think we have
confounded two issues in this thread, the internal
representation/accuracy, and the final presentation. They aren't
really the same thing, unless we force them to be (i.e. using ints).

-- 
Keith
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-25 Thread Mark Lawrence

On 26/01/2014 01:55, Steven D'Aprano wrote:


This is an international forum, and English an international language
with many slight differences between variations and dialects. Even in
American English alone, there are ambiguous terms. "Coke" could mean a
beverage by the Coca-Cola company, a generic or rival cola beverage, a
generic carbonated beverage of arbitrary flavour, an illegal drug, or a
type of coal.

http://strangemaps.wordpress.com/2008/08/18/308-the-pop-vs-soda-map/

Somehow Americans cope with that. They can learn to cope with the many
flavours of brackets as well :-)



Still my favourite 
http://english.stackexchange.com/questions/16285/what-is-the-meaning-of-the-expression-we-can-table-this 
as it caused a row between UK and US commanders during WWII.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-25 Thread Steven D'Aprano
On Sat, Jan 25, 2014 at 02:39:11PM -0500, bob gailer wrote:
> On 1/24/2014 10:28 PM, bob gailer wrote:
> 
> Sorry for misspelling parens.
> 
> My reason for requesting the various names is that it makes 
> communication clear, explicit and terse.
> 
> When someone says just "brackets" what does he actually mean?

It's possible to write ambiguous or unclear sentences about anything, 
not just brackets. Singling out them out for special treatment makes 
little sense to me. The nature of the English language is that we can 
write unclear sentences:

 "And then she told her that she knew that he said that she knew
 about that time he and she kissed at a party..."

How many people are involved? This is an extreme case, exaggerated for 
effect, but people do speak like that and given a little bit of context 
people are usually pretty good at disambiguation. Compared to that, 
inferring the required type of bracket is usually trivial.

If I'm talking to other Australians, I'll generally use "bracket" on its 
own to mean round () brackets, as that's the normal use here. In an 
international context, it will be either obvious from context, or 
generic and apply equally to any sort of bracket.

E.g. if I'm talking about a line of code that says 

print(mylist.index(None)

and say "you're missing the closing bracket", is it really so confusing 
to infer that it's a closing ROUND bracket ) rather than a square 
bracket ] that is needed? Even a beginner should be able to work that 
out.

But I'm only human, and it is possible that at some point I'll make a 
mistake and write a confusing sentence where the meaning cannot be 
inferred, whether that's about brackets or something else doesn't 
matter. If you, or anyone else, catches me making a *specific* ambiguous 
statement that is unclear, regardless of whether it is due to the word 
"bracket" or not, then I welcome people asking me to clarify.

This is an international forum, and English an international language 
with many slight differences between variations and dialects. Even in 
American English alone, there are ambiguous terms. "Coke" could mean a 
beverage by the Coca-Cola company, a generic or rival cola beverage, a 
generic carbonated beverage of arbitrary flavour, an illegal drug, or a 
type of coal.

http://strangemaps.wordpress.com/2008/08/18/308-the-pop-vs-soda-map/

Somehow Americans cope with that. They can learn to cope with the many 
flavours of brackets as well :-)

> For more grins see 
> http://www.codinghorror.com/blog/2008/06/ascii-pronunciation-rules-for-programmers.html
> and http://www.theasciicode.com.ar/

Nice :-)



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-25 Thread Steven D'Aprano
On Sat, Jan 25, 2014 at 09:11:56AM +0100, spir wrote:

> As a foreigner, I noticed that english native speakers use both the series 
> round / square / curly / angle brackets, and individual terms parens (no 
> 'd' ;-) / brackets / braces / chevrons. No major issue, except for 
> 'brackets' which can be either a collective term or specific to [].

In the UK and Australia, "brackets" on its own refers to round brackets 
(parentheses), as they are the most common form. We never use "brackets" 
on its own to mean [], but only (), and the only time we bother to say 
"round brackets" is when there is a need to disambiguate them from 
square ones.


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread spir

On 01/25/2014 10:38 PM, Keith Winston wrote:

Also, just to be clear: I'd suggest floats because decimal requires
importing a module and using the non-built-in features thereof


The issue is not that much whether it's supported by builtins, in software, but 
by CPU's; which is not the case, so that all computations are made in 
software... there used to be some machines with builtin decimals (internal 
representation), maybe there still are some, but certainly not in mainstream 
computers.


d
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread spir

On 01/25/2014 10:19 PM, Keith Winston wrote:

On Sat, Jan 25, 2014 at 3:57 AM, spir  wrote:

Note: AFAIK most financial software use integers for this reason and to
avoid (or control) rounding errors.


I don't think this is true (no flame intended, hopefully you know I'm
forever in your debt Denis): there's a famous scam where insiders at a
major financial institution set the software so it always rounds down,
and the difference was deposited in their account. It was a matter of
fractions of pennies on a per-scam basis. I'm not certain if this ever
actually happened (I thought it did, but Snopes seems agnostic).

http://www.snopes.com/business/bank/salami.asp


There's a similar story in France as well, but it was several decades ago. 
Incendentally, I used to know several people involved in financial software 
companies; they did use integers for all computations. Maybe today they use 
decimals, dunno (since then, decimal libs have become far more famous and used).


d
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread spir

On 01/25/2014 10:01 PM, Keith Winston wrote:

On Sat, Jan 25, 2014 at 3:57 AM, spir  wrote:

.009 to the price, so that people do not have to type the full amount.
   Example, 3.49 /gallon would return 3.499 /gallon.

This is what I have tried and the results of it.

def gas_price(price):
 price == raw_input("What is the price of gas?")  return price + .09
3.49=> 3.4898


I think there's an inconsistency in your post that might confuse the
answers. You mention in the lead that you want to add 9 ONE HUNDREDTHS
of a dollar, or tenths of a cent (which is in fact how gas is priced
in the US, and yes it's crazy stupid). However in your example you add
only tenths, but then in the answer you appear to have added
hundredths, which makes me think that you didn't cut & paste, but
rather retyped (and mistyped).

This will make it a little trickier to use Denis' last idea of using
integers, since you'll have to take them out one more order of
magnitude.


I guess this is what I wrote (unit was 1/10 cent), or maybe I misunderstand your 
point.



 If this exercise is later followed by interest
calculations, or anything like that, you might regret limiting your
internal accuracy/representation.

I think that you should probably do your math in floating point (why
get complicated? And you might need the accuracy, for hundredths of
dollars and interest) and then format the output to be what you want.
Watch out for rounding.


p = 3.499
print('{0:.3f}'.format(p))   # format a float with 3 places after the decimal

3.499

p = 3.4994
print('{0:.3f}'.format(p))

3.499

p = 3.499
print('{0:.3f}'.format(p))

3.500


Yes; but this only corrects output, fo the user's comfort. If you need to do 
further computations, then the internal representation must also be right (else 
you'll get at best rounding errors, at worst worse ;-), and this can only be 
ensured by computing on integers or decimals. Typically, using integers, you'll 
choose a unit a few orders of magnitude lower than the most precise numbers 
possibly involved in computations (eg, a tax rate of 0.1234567 ;-).


d
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread Oscar Benjamin
On 25 January 2014 21:38, Keith Winston  wrote:
>
> Also, just to be clear: I'd suggest floats because decimal requires
> importing a module and using the non-built-in features thereof,

Importing a module is not something to be afraid of. Python comes with
loads of modules especially so you can import them! :P

Also as of CPython 3.3 the decimal module is rewritten in C (which is
what is normally meant by "built-in").

> especially if you're going to do something like
> decimal.getcontext().prec (even that doesn't set precision AFTER the
> decimal point... only total precision). My point being that I don't
> see the value of sending a beginner into that morass.

Maybe it's tricky for a beginner. It is however good advice that you
should not use binary floating point for financial calculations.

> I actually still
> can't find how to force decimal to round to 3 places after the
> decimal...

I find myself using this:

>>> from decimal import Decimal as D
>>> D('0.1234567').quantize(D('1.000'))
Decimal('0.123')

Perhaps it would be better though to point at this:
>>> round(D('0.123456'), 3)
Decimal('0.123')

> (actually, I can't find a single reference to .prec except
> in examples in the standard library doc of decimal
> http://docs.python.org/3.3/library/decimal.html#decimal.getcontext)

.prec is "significant figures" rather than "decimal places" so it's
not what you want here.

> And finally: when I said "watch out for rounding", I meant be prepared
> for it. It is correct that rounding happens (otherwise the Snopes
> Salami scam happens), and I can't imagine that at the level at which
> the OP is working, any subtle control over rounding will be important
> (i.e. ROUNDING_CEILING or any such thing).

Some calculations need rounding in any fixed-width floating point
system. The decimal module let's you go up to 425 million digits
though:
>>> from decimal import MAX_PREC
>>> MAX_PREC
42500

More importantly it has traps that you can use to catch any example of
inexact computation:

>>> from decimal import localcontext, Inexact
>>> with localcontext() as ctx:
... ctx.traps[Inexact] = True
... D(1) / 3
...
Traceback (most recent call last):
  File "", line 3, in 
decimal.Inexact: []

This way you can feel confident that your results are exact or else
you'd have seen an error message.


Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread Alan Gauld

On 25/01/14 21:19, Keith Winston wrote:

On Sat, Jan 25, 2014 at 3:57 AM, spir  wrote:

Note: AFAIK most financial software use integers for this reason and to
avoid (or control) rounding errors.


I don't think this is true


I can't speak for the general case but the only major financial app I've 
worked on (a mainframe billing system in COBOL) was done in pennies. It 
was a major rewrite of a legacy system  for the

"Millenium Bug". (It used the year 00 as a special case for
terminated accounts!! Amongst other foibles)...

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread Keith Winston
Also, just to be clear: I'd suggest floats because decimal requires
importing a module and using the non-built-in features thereof,
especially if you're going to do something like
decimal.getcontext().prec (even that doesn't set precision AFTER the
decimal point... only total precision). My point being that I don't
see the value of sending a beginner into that morass. I actually still
can't find how to force decimal to round to 3 places after the
decimal... (actually, I can't find a single reference to .prec except
in examples in the standard library doc of decimal
http://docs.python.org/3.3/library/decimal.html#decimal.getcontext)

And finally: when I said "watch out for rounding", I meant be prepared
for it. It is correct that rounding happens (otherwise the Snopes
Salami scam happens), and I can't imagine that at the level at which
the OP is working, any subtle control over rounding will be important
(i.e. ROUNDING_CEILING or any such thing).

Keith
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-25 Thread Alan Gauld

On 25/01/14 19:39, bob gailer wrote:

On 1/24/2014 10:28 PM, bob gailer wrote:

Sorry for misspelling parens.

My reason for requesting the various names is that it makes
communication clear, explicit and terse.

When someone says just "brackets" what does he actually mean?


In UK English speaking places 'brackets' by default means what
US English speakers call parentheses.

Whereas parentheses means any kind of parenthetical
expression which includes dashes, quotes and any kind of bracket
etc.

This was one of the biggest surprises when I first wrote a book
for a US publisher. One reviewer went so far as to suggest
that my level of "illiteracy" meant the book should be
rejected, because if I spelt parentheses as brackets how
could I possibly know anything about programming...

I actually intended pulling together a list of all the
Americanisms that I had to modify in my original text but
deadlines got in the way and I never got round to it.
But I was amazed at how many language changes were
necessary.

Language is a funny thing.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread Oscar Benjamin
On 25 January 2014 21:01, Keith Winston  wrote:
>
> I think that you should probably do your math in floating point (why
> get complicated? And you might need the accuracy, for hundredths of
> dollars and interest) and then format the output to be what you want.
> Watch out for rounding.

It may not matter in this case but the key point about the Decimal
module is that it implements "decimal floating point" whereas ordinary
floats implement "binary floating point". I would prefer to use the
decimal module which can represent decimal values exactly and perform
simple decimal-type computations such as this one exactly.

Incidentally you can use the Decimal module to see how the *exact*
value of a float looks when written out in decimal digits:

>>> from decimal import Decimal as D
>>> D(0.5) # 0.5 can be represented exactly in binary FP
Decimal('0.5')
>>> D(0.1) # 0.1 cannot
Decimal('0.155511151231257827021181583404541015625')
>>> D(3.49)
Decimal('3.4902131628207280300557613372802734375')
>>> D(3.499)
Decimal('3.499110134124042815528810024261474609375')


Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread Keith Winston
On Sat, Jan 25, 2014 at 3:57 AM, spir  wrote:
> Note: AFAIK most financial software use integers for this reason and to
> avoid (or control) rounding errors.

I don't think this is true (no flame intended, hopefully you know I'm
forever in your debt Denis): there's a famous scam where insiders at a
major financial institution set the software so it always rounds down,
and the difference was deposited in their account. It was a matter of
fractions of pennies on a per-scam basis. I'm not certain if this ever
actually happened (I thought it did, but Snopes seems agnostic).

http://www.snopes.com/business/bank/salami.asp

-- 
Keith
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread Keith Winston
On Sat, Jan 25, 2014 at 3:57 AM, spir  wrote:
>> .009 to the price, so that people do not have to type the full amount.
>>   Example, 3.49 /gallon would return 3.499 /gallon.
>>
>> This is what I have tried and the results of it.
>>
>> def gas_price(price):
>> price == raw_input("What is the price of gas?")  return price + .09
>>3.49=> 3.4898

I think there's an inconsistency in your post that might confuse the
answers. You mention in the lead that you want to add 9 ONE HUNDREDTHS
of a dollar, or tenths of a cent (which is in fact how gas is priced
in the US, and yes it's crazy stupid). However in your example you add
only tenths, but then in the answer you appear to have added
hundredths, which makes me think that you didn't cut & paste, but
rather retyped (and mistyped).

This will make it a little trickier to use Denis' last idea of using
integers, since you'll have to take them out one more order of
magnitude. If this exercise is later followed by interest
calculations, or anything like that, you might regret limiting your
internal accuracy/representation.

I think that you should probably do your math in floating point (why
get complicated? And you might need the accuracy, for hundredths of
dollars and interest) and then format the output to be what you want.
Watch out for rounding.

>>> p = 3.499
>>> print('{0:.3f}'.format(p))   # format a float with 3 places after the 
>>> decimal
3.499
>>> p = 3.4994
>>> print('{0:.3f}'.format(p))
3.499
>>> p = 3.499
>>> print('{0:.3f}'.format(p))
3.500

-- 
Keith
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread Oscar Benjamin
On 25 January 2014 08:57, spir  wrote:
> On 01/25/2014 09:46 AM, spir wrote:
>>
>> On 01/24/2014 06:57 PM, Leon S wrote:
>>>
>>> Here is what I'm trying to do, accept a price of gas, but I want to add
>>> the
>>> .009 to the price, so that people do not have to type the full amount.
>>>   Example, 3.49 /gallon would return 3.499 /gallon.
>>>
>>> This is what I have tried and the results of it.
>>>
>>> def gas_price(price):
>>> price == raw_input("What is the price of gas?")  return price + .09
>>>3.49=> 3.4898
>>>
>>> It reduces the number and then adds many decimal points after.
>>>
>>> Thanks for any help, I am sure this is an easy one for someone.
>>
>> This is instead easy for noone ;-)
>>
>> The core issue is that for, say, "fractional numbers" (numbers with a
>> fractional
>> part, but unlike real numbers with definite precision) python like most
>> programming languages uses in standard a binary representation internally.
>> While
>> we normally use decimal notation, both in input (reading) and output
>> (writing).
>> And there is no way to represent decimal fractions in binary, except in
>> the very
>> case where they are a multiple of an exact (negative) power of 2 (for
>> instance
>> 1/2, 3/4, 123/128... are ok).
>>
>> The only right solution is to use decimals internally, and python provides
>> such
>> a numeric type, Decimal:
>> http://docs.python.org/3/library/decimal.html
>
> I did not read correctly. If you're dealing with financial as it seems, the
> right way is to use integers instead.

I would say that using Decimal is the "right" solution for financial
calculations. Using integers (AKA fixed-point arithmetic) is possible
and is the standard solution for languages that lack a Decimal type.

> Since you are adding tenth's of pence,
> this is what your unit means. Then your sum is:
> 3490 + 9
> :-)
>
> Note: AFAIK most financial software use integers for this reason and to
> avoid (or control) rounding errors.

The Decimal module allows you to avoid or control rounding errors. If
it is true that most financial software uses fixed-point arithmetic
instead of floating point decimal arithmetic then I would guess that
the most likely reason is that the language used for the software
doesn't have a library for floating point decimal arithmetic as Python
does.

A significant motivating use-case for adding the Decimal module and
for subsequently speeding it up with a C implementation was to support
using Python in the context of financial software.

> At input and/or output, you may still have to convert to Decimal to get
> "decimally correct" value or expression.
>
> numeral = input("...")
> value = Decimal(numeral)
> ...
> output = Decimal(result) / Decimal("1000")
> print(output)
>
> (not sure, not tried)

I would do it like so:

>>> from decimal import Decimal
>>> def adjust_price(price):
... return Decimal(price) + Decimal('.005')
...
>>> adjust_price('3.49')
Decimal('3.495')
>>> print(adjust_price('3.49'))
3.495

But then what happens if the use has added the .005 themselves:

>>> print(adjust_price('3.495'))
3.500

Perhaps we should round down before adding .005:

>>> from decimal import ROUND_DOWN
>>> def adjust_price(price):
... # Round down to 2 decimal places
... price = Decimal(price).quantize(Decimal('1.00'), rounding=ROUND_DOWN)
... return price + Decimal('.005')
...
>>> print(adjust_price('3.49'))
3.495
>>> print(adjust_price('3.495'))
3.495

Of course now you'll get:

>>> print(adjust_price('3.497'))
3.495

Maybe that's not what was wanted either (I'm not sure exactly what you
do want in this case).


Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-25 Thread bob gailer

On 1/24/2014 10:28 PM, bob gailer wrote:

Sorry for misspelling parens.

My reason for requesting the various names is that it makes 
communication clear, explicit and terse.


When someone says just "brackets" what does he actually mean?

For more grins see 
http://www.codinghorror.com/blog/2008/06/ascii-pronunciation-rules-for-programmers.html

and http://www.theasciicode.com.ar/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-25 Thread Mark Lawrence

On 25/01/2014 03:28, bob gailer wrote:


And please call () parends and [] brackets, and{} braces. Saves a lot of
confusion.



Not in the UK or Australia, with the former being where English came from.

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread spir

On 01/25/2014 09:46 AM, spir wrote:

On 01/24/2014 06:57 PM, Leon S wrote:

Here is what I'm trying to do, accept a price of gas, but I want to add the
.009 to the price, so that people do not have to type the full amount.
  Example, 3.49 /gallon would return 3.499 /gallon.

This is what I have tried and the results of it.

def gas_price(price):
price == raw_input("What is the price of gas?")  return price + .09
   3.49=> 3.4898


It reduces the number and then adds many decimal points after.


Thanks for any help, I am sure this is an easy one for someone.


This is instead easy for noone ;-)

The core issue is that for, say, "fractional numbers" (numbers with a fractional
part, but unlike real numbers with definite precision) python like most
programming languages uses in standard a binary representation internally. While
we normally use decimal notation, both in input (reading) and output (writing).
And there is no way to represent decimal fractions in binary, except in the very
case where they are a multiple of an exact (negative) power of 2 (for instance
1/2, 3/4, 123/128... are ok).

The only right solution is to use decimals internally, and python provides such
a numeric type, Decimal:
http://docs.python.org/3/library/decimal.html


I did not read correctly. If you're dealing with financial as it seems, the 
right way is to use integers instead. Since you are adding tenth's of pence, 
this is what your unit means. Then your sum is:

3490 + 9
:-)

Note: AFAIK most financial software use integers for this reason and to avoid 
(or control) rounding errors.


At input and/or output, you may still have to convert to Decimal to get 
"decimally correct" value or expression.


numeral = input("...")
value = Decimal(numeral)
...
output = Decimal(result) / Decimal("1000")
print(output)

(not sure, not tried)

d
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread spir

On 01/24/2014 06:57 PM, Leon S wrote:

Here is what I'm trying to do, accept a price of gas, but I want to add the
.009 to the price, so that people do not have to type the full amount.
  Example, 3.49 /gallon would return 3.499 /gallon.

This is what I have tried and the results of it.

def gas_price(price):
price == raw_input("What is the price of gas?")  return price + .09
   3.49=> 3.4898


It reduces the number and then adds many decimal points after.


Thanks for any help, I am sure this is an easy one for someone.


This is instead easy for noone ;-)

The core issue is that for, say, "fractional numbers" (numbers with a fractional 
part, but unlike real numbers with definite precision) python like most 
programming languages uses in standard a binary representation internally. While 
we normally use decimal notation, both in input (reading) and output (writing). 
And there is no way to represent decimal fractions in binary, except in the very 
case where they are a multiple of an exact (negative) power of 2 (for instance 
1/2, 3/4, 123/128... are ok).


The only right solution is to use decimals internally, and python provides such 
a numeric type, Decimal:

http://docs.python.org/3/library/decimal.html

d
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-25 Thread spir

On 01/25/2014 05:14 AM, Steven D'Aprano wrote:

On Fri, Jan 24, 2014 at 10:28:09PM -0500, bob gailer wrote:


And please call () parends and [] brackets, and{} braces. Saves a lot of
confusion.


If you think that parentheses are spelt with a "d", you're certainly
confused :-)

They're all brackets. Often the type of bracket doesn't matter, but when
it does, adjectives do a perfectly fine job at distinguishing one from
the other: round brackets, square brackets, and curly brackets are
well-known and in common use all over the Commonwealth, and have been
established since the mid 1700s.

As a sop to Americans, who I understand are easily confused by ordinary
English *wink*, the Unicode consortium describes () as parentheses:

py> unicodedata.name("(")
'LEFT PARENTHESIS'

but [] and {} are described as brackets:

py> unicodedata.name("[")
'LEFT SQUARE BRACKET'
py> unicodedata.name("{")
'LEFT CURLY BRACKET'

As are angle brackets:

py> unicodedata.lookup("LEFT ANGLE BRACKET")
'〈'
py> unicodedata.lookup("RIGHT ANGLE BRACKET")
'〉'


As a foreigner, I noticed that english native speakers use both the series round 
/ square / curly / angle brackets, and individual terms parens (no 'd' ;-) / 
brackets / braces / chevrons. No major issue, except for 'brackets' which can be 
either a collective term or specific to [].



HTML uses ASCII less-than and greater-than signs as angle brackets.

Physicists even have a pun about them, with "bra-ket" notation for
quantum state:

http://en.wikipedia.org/wiki/Bra-ket_notation


funny


There are a number of other types of brackets with more specialised
uses, or common in Asian texts. See http://en.wikipedia.org/wiki/Bracket

By the way, the word "bracket" itself is derived from the French and
Spanish words for "codpiece". That's not relevant to anything, I just
thought I'd mention it.


Apparently, according to wiktionary, it may come from an old germanic root 
through Gaulish:

https://en.wiktionary.org/wiki/bracket#Etymology
<< Etymology

From earlier bragget, probably from Middle French braguette, from Old French 
braguette (“the opening in the fore part of a pair of breeches”), from Old 
Provençal braga, from Latin brāca (“pants”), from Transalpine Gaulish *brāca 
(“pants”), perhaps from or related to similar forms in Germanic: compare Old 
English braccas (“pants”), Old English brōc (“breeches”), from 
Proto-Indo-European *bʰrāg-, from *bʰreg- (“to break, crack, split, divide”). 
More at breech, britches. >>


d


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor