Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Fun with numbers - dammit, but I want a cast!

Reply
Thread Tools

Fun with numbers - dammit, but I want a cast!

 
 
Graham Nicholls
Guest
Posts: n/a
 
      08-11-2003
Hi.

I'm having some fun with numbers. I've extraced an image sizes from a jpeg
file

img_x,img_y=image.getsize()

then I'm trying to use those sizes to scale the image, but because python
has decided that they are integers, I keep getting division by zero errors

eg
xscale=xframe/img_x

where xframe will be say 300, and img_x will be 1800
xscale has the value 0.

I've tried doing img_x=1.0 to force it to be a float, but I'm getting a bit
frustrated as it seems to make no difference - once image.getsize returns
the (integer) value of the x size, it simply converts back to an int. I've
tried this at the command line, and it seems to confirm that behaviour -
eg:
graham@rocklap:~/work/hsb/pdflive> python
Python 2.3b1 (#3, Jun 17 2003, 23:06:11)
[GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x=1.0
>>> xf=1.0
>>> scale=1.0
>>> x=1800
>>> xf=300
>>> scale=xf/x
>>> scale

0
>>>





Heres the relevant code in full:

img_x=1.0
img_y=1.0
img_x,img_y=image.getsize()
except "Not_JPEG":
if warn:
print ("WARNING: Image file %s is not a jpeg file" % fname)
sys.exit(OPEN_ERR)
# How many pixels per mm do we have
# On a4 paper, using pdfrw ? Docs seem to suggest between 60-160
# which seems a lot.

xscale=1.0
yscale=1.0
scale=1.0
xscale=1/(xframe/img_x)
yscale=1/(yframe/img_y)
#import pdb
#pdb.set_trace()
print ("xscale=%f,yscale=%f" %(xscale,yscale))
scale=min(xscale,yscale) * 100
print ("xframe=%d,yframe=%d, x=%d,y=%d scale=%f\n" %(xframe, yframe,
img_x, img_y, scale))

I'd really appreciate some help, thanks!
Graham
--
Graham Nicholls
Rock Computer Consultancy

 
Reply With Quote
 
 
 
 
Charl P. Botha
Guest
Posts: n/a
 
      08-11-2003
In article <3f378fac$0$10778$(E-Mail Removed) t>,
Graham Nicholls wrote:
> then I'm trying to use those sizes to scale the image, but because python
> has decided that they are integers, I keep getting division by zero errors
>
> eg
> xscale=xframe/img_x
>
> where xframe will be say 300, and img_x will be 1800
> xscale has the value 0.


Have you tried doing it this way:
xscale = xframe / float(img_x)

--
charl p. botha http://cpbotha.net/ http://visualisation.tudelft.nl/
 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      08-11-2003
Graham Nicholls <(E-Mail Removed)> wrote in
news:3f378fac$0$10778$(E-Mail Removed) t.net:

> eg
> xscale=xframe/img_x
>
> where xframe will be say 300, and img_x will be 1800
> xscale has the value 0.
>
> I've tried doing img_x=1.0 to force it to be a float, but I'm getting
> a bit frustrated as it seems to make no difference - once
> image.getsize returns the (integer) value of the x size, it simply
> converts back to an int.


Variables don't have types in Python, objects have types. Assigning a float
object to a variable doesn't have any effect on the type of the next
object assigned to the same variable.

To get the behaviour you want either convert at least one of your values to
a float:

xscale = float(xframe)/img_x

or add the following 'magic' line to the top of your sourcefile:

from __future__ import division

If your file has that line at the top, division will behave more the way
you expect: casting to float automatically whenever you divide integers.

--
Duncan Booth http://www.velocityreviews.com/forums/(E-Mail Removed)
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
 
Reply With Quote
 
Richard Brodie
Guest
Posts: n/a
 
      08-11-2003

"Graham Nicholls" <(E-Mail Removed)> wrote in message
news:3f378fac$0$10778$(E-Mail Removed) t.net...

> I've tried doing img_x=1.0 to force it to be a float, but I'm getting a bit
> frustrated as it seems to make no difference - once image.getsize returns
> the (integer) value of the x size, it simply converts back to an int.


Yes. Types are properties of values, not names. There is no implied
declaration: you can assign a float to img_x then later an int, string,
function or whatever.

To get floating point division you can use a cast like syntax, by using
the builtin float() function.

>>> 1 / 2

0
>>> float (1) / float(2)

0.5

In future versions of Python, / will become a floating point division operator.
// will be used for truncating division. For compatibility, you need to
explicitly enable the new behaviour at present, either with a from __future__
or a -Q option. These activate Guido's famous time machine, which add
new features to Python before you ask for them.

>>> from __future__ import division
>>> 1 / 2

0.5
>>> 1 // 2

0

U:\>python -Qwarn
ActivePython 2.2.2 Build 224 (ActiveState Corp.) based on
Python 2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/2

__main__:1: DeprecationWarning: classic int division
0


 
Reply With Quote
 
Torsten Marek
Guest
Posts: n/a
 
      08-11-2003
Graham Nicholls schrieb:
> Hi.
>
> I'm having some fun with numbers. I've extraced an image sizes from a jpeg
> file
>
> img_x,img_y=image.getsize()
>
> then I'm trying to use those sizes to scale the image, but because python
> has decided that they are integers, I keep getting division by zero errors
>
> eg
> xscale=xframe/img_x
>
> where xframe will be say 300, and img_x will be 1800
> xscale has the value 0.
>
> I've tried doing img_x=1.0 to force it to be a float, but I'm getting a bit
> frustrated as it seems to make no difference - once image.getsize returns
> the (integer) value of the x size, it simply converts back to an int. I've
> tried this at the command line, and it seems to confirm that behaviour -
> eg:
> graham@rocklap:~/work/hsb/pdflive> python
> Python 2.3b1 (#3, Jun 17 2003, 23:06:11)
> [GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>
>>>>x=1.0
>>>>xf=1.0
>>>>scale=1.0
>>>>x=1800
>>>>xf=300
>>>>scale=xf/x
>>>>scale

>
> 0
>
>
>
>
>
> Heres the relevant code in full:
>
> img_x=1.0
> img_y=1.0
> img_x,img_y=image.getsize()
> except "Not_JPEG":
> if warn:
> print ("WARNING: Image file %s is not a jpeg file" % fname)
> sys.exit(OPEN_ERR)
> # How many pixels per mm do we have
> # On a4 paper, using pdfrw ? Docs seem to suggest between 60-160
> # which seems a lot.
>
> xscale=1.0
> yscale=1.0
> scale=1.0
> xscale=1/(xframe/img_x)
> yscale=1/(yframe/img_y)
> #import pdb
> #pdb.set_trace()
> print ("xscale=%f,yscale=%f" %(xscale,yscale))
> scale=min(xscale,yscale) * 100
> print ("xframe=%d,yframe=%d, x=%d,y=%d scale=%f\n" %(xframe, yframe,
> img_x, img_y, scale))
>
> I'd really appreciate some help, thanks!
> Graham

The type of a variable does not depend on the type it was initialized with.
You could do
t = 1
t = 1.1
t = "1"
t = [1,]
and the type changes each time. There is no concept of "int xscale" what
you might have in mind.
You just should convert the integer into a float with
xscale=1/(xframe/float(img_x)).


greetings
Torsten

 
Reply With Quote
 
Istvan Albert
Guest
Posts: n/a
 
      08-11-2003
Graham Nicholls wrote:

>>Have you tried doing it this way:
>>xscale = xframe / float(img_x)

>
>
> Thats what I wanted to do, but was sure I'd read that python didn't have
> casts, and that _looks_ like a cast to me!


You can also force floating point math by muliplying with 1.0
before the division:

xscale = 1.0 * xframe/img_x

Istvan.

 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      08-11-2003
Graham Nicholls wrote:
...
>> xscale = xframe / float(img_x)

>
> Thats what I wanted to do, but was sure I'd read that python didn't have
> casts, and that _looks_ like a cast to me!


Well, Python surely has the ability to create new objects, and the most
typical way to do that is to call a type, possibly passing it, as the
call's arguments, the value[s] that direct the new object's creation.


So, for example, if you have a string S and want to create a list L
whose items are the string's characters, you typically code:

L = list(S)


Similarly, if you have a number N and want to create a float F whose
value is the floating-point equivalent of N's value, you code:

F = float(N)


Whether these are "casts" is, I guess, a pretty moot issue. Me, I'd
call them "type calls" (or "explicit constructor calls" if I were in a C++
mood, reserving the terminology "cast" for the C/Java notation:

(sometype)somevalue

or the C++ notations of forms such as:

static_cast<sometype>(somevalue)

But, of course (in C++), explicitly calling a costructor of (e.g.) float,
with an integer argument; or statically casting an int to float; or even
using the old C-ish "prepended type in parenthesis" notation; have much
the same effect in most contexts. As long as you're quite clear that
what you're actually doing is "create a new value of a specified type
by calling the type with suitable argument[s]", rather than (the typical
idea of "cast") "reinterpreting an existing value AS IF it was of some
other type rather than of the type it actually is", it may not be a
problem if you like to call the operation "a cast".


Alex

 
Reply With Quote
 
Graham Nicholls
Guest
Posts: n/a
 
      08-11-2003
Istvan Albert wrote:

> Graham Nicholls wrote:
>
>>>Have you tried doing it this way:
>>>xscale = xframe / float(img_x)

>>
>>
>> Thats what I wanted to do, but was sure I'd read that python didn't have
>> casts, and that _looks_ like a cast to me!

>
> You can also force floating point math by muliplying with 1.0
> before the division:
>
> xscale = 1.0 * xframe/img_x
>
> Istvan.

Thanks everybody! I'm now sorted with numbers. At least 2.0+2.0 now equals
about 4.0, anyway.
Graham
--
Graham Nicholls
Rock Computer Consultancy

 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      08-11-2003
Graham Nicholls wrote:

> Thats what I wanted to do, but was sure I'd read that python didn't
> have
> casts, and that _looks_ like a cast to me!


It's a function call, not a conversion. Even in C++, conversion by
constructor is not considered a cast. A cast would be:

xframe/static_cast<float>(image_x)

and there is no analogous structure to this in Python.

--
Erik Max Francis && (E-Mail Removed) && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Nobody can reach me, nobody can touch me; it's a wonderful feeling.
\__/ Aaliyah
 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      08-11-2003
Terry Reedy wrote:

> No, quite different. A C cast (at least usually) recasts a block of
> bits in a new role without changing the bits. Type constructors
> (unless redundant as in int(1)) return a different object with a
> different set of bits.


Well, the issue is that, in C, a cast can mean a lot of different
things. That's why, in C++, casts are separated into template like
syntaxes depending on their purpose. The unsafe form of C cast is a
reinterpret_cast in C++, and sometimes a const_cast (if you actually
cast away constness of a truly const object and then attempt to mutate
it). The other two -- static_cast and dynamic_cast -- are "safe" in the
sense of being well-defined when the compiler accepts them, and merely
invoke a well-defined conversion and do a runtime type cast,
respectively.

--
Erik Max Francis && (E-Mail Removed) && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Nobody can reach me, nobody can touch me; it's a wonderful feeling.
\__/ Aaliyah
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OT Thursday, uh, fun, yeah, fun! Consultant MCSE 17 02-10-2007 03:39 AM
3 PIX VPN questions - FUN FUN FUN frishack@gmail.com Cisco 3 03-16-2006 02:25 PM
OT: Wednesday follow-up-to-Tuesday-Fun Fun Ken Briscoe MCSE 0 07-14-2004 01:41 PM
Programming is not as much fun/more fun than it used to be. Andy Fish Java 65 05-18-2004 08:24 PM
Fun fun fun Luke Computer Support 3 10-07-2003 03:45 PM



Advertisments