Pre-defined functions

  • Susan Thomson
    Participant

    I am just developing Python materials for the revised course. Can anyone give me an example of when you would need to use ‘convert floating-point numbers to integers’ in a program. Normally, it would be better to use ’round’. Are there instances when conversion to integer would be better?

    Mrs Janet McDonald
    Participant

    The dreaded Q.21 in the 2015 Nat 5 paper (the horrible one about the number of bricks needed to build a wall) is a good example of when you might use Int instead of Round. Even if it comes out as 49.3 bricks, you need 50, not 49 (which you would get if you rounded it), so the solution uses int(result)+1 to achieve this.

    Scott Leiper
    Participant

    Could be to allow a more robust inputting of integer numbers. Accept them as floats and cast them to integers after validation? But as @j.mcdonald said, if I was selling whole numbers of things, I would need to always round up to be correct, so x = int(x)+1

    Peter Thoresen
    Participant

    Python 3.6 has several functions that will “convert floating-point numbers to integers, each with their own uses:

    myInt = int(myFloat)
    This truncates the decimal points, effectively rounding numbers towards zero

    myInt = round(myFloat)
    Round to nearest integer – with 0.5’s rounding to the even choice 0.5–>0 , 1.5–>2, 2.5–>2, 3.5–>4
    Apparently, pupils in maths are taught to always round with 0.5s up. (https://www.bbc.com/education/guides/zw64jxs/revision/2)
    I prefer the Python method as, over a large data set, the rounding up/down of 0.5s will cancel

    myFloat2 = round(myFloat,0)
    Do not use a second parameter of 0 with the round function as this will return another (rounded) floating point number

    myInt = math.floor(myFloat)
    Round a number down

    myInt = math.ceil(myFloat)
    Round a number up

    Which, or all, of these, is expected is open to the exam and coursework writers’ interpretation of the course specification.

    Susan Thomson
    Participant

    Many thanks.

    Susan

    kerry mcmahon
    Participant

    Hi Susan,

    I came across your post as i wanted to check that the “converting floating point to integer” statement in the arrangements simply referred to taking a real number such as 23.436 and converting it to 23.

    so, with that in mind, I was just creating a programming task to teach the use of modulus and quite by dumb luck, my task happens to make use of conversion from a floating point number to an integer.

    i was making a binary converter using the old method of dividing the number you want to convert, repeatedly by 2, keeping note of the remainder each time. The list of remainders is the read from end to start/bottom to top, giving the binary conversion. The important part of the code was as follows:

    value=int(input(“please enter a value to convert to binary \n”))
    while value >1:
    remainder= value%2 #% gives the modulus in python
    value=int(value /2) # we need to convert value after division to
    keep it a whole number
    binary.append(remainder) #adding the remainder to an array called
    binary

    Hope that makes sense and lets you kill two birds with one stone.

    kerry

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.