Thread: Fastest float to int conversion

  1. #16
    Registered User
    Join Date
    Oct 2004
    Posts
    36
    Salem, still a happy camper I see.

    This is a personal project, so I am hardly going to hire somebody. Also, if someone asks a question like "how do I step through all the letters in my name in the quickest time, its quite long you see" - I'm not going to say - "Why the hell do you have a long name in the first place dumbass".

    My code in this instance involves moving objects around at arbitrary angles. This involves computing a dx and dy step based on the sin and cos of the angle (LUT), and then stepping a sprite by that amount every 'frame'. Floats are used to keep the movement accurate.

    The float->int conversion is needed when the sprite coordinates are transformed to screen space, as obviously I can't place something at x=10.5, y=8.756 !

    floats are also used in various other parts of my code, and although in some instances I can probably make use of integers<<5 (for example a resolution of 1/32 pixels) it would make the code messy, and I can't do this in all instances.

    So in the instances that I DO have to convert a float to an int, which method would be the fastest?

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So why don't you just multiply everything by 1000 for precision, and divide by 1000 when you need to get back to "whole numbers"? Let's see, 32bit integers give you roughly four billion, divided by 1000, means you can use numbers up to four million with three trailing decimal places. 400,000 will give you for places of precision, and 40,000 will give you five.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I'll admit little knowledge, but it seems that if your x-value is 10.5 and your y-value is 8.756 and you want to convert to an int, then why use floating point arithmetic to calculate the x- and y-values to begin with? Why not just use integer arithmetic and not care about the floating point, since you're just going to throw it away later?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #19
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    Yup. I'd use a power of 2, say 1024 to do like quzah says. Then you only need to shift & mask to get the integer part. A lot faster than casting from float!

  5. #20
    Registered User
    Join Date
    Oct 2004
    Posts
    36
    @Jez - Yip, that was one of my theories. (However, quazah doesn't mention a power of 2, or bit shifting, but rather division - which is hell slow).

    @pianorain - This is because say the step size was 1.3, and x started at 0, I would want something like this:

    0: x = 0
    1: x = 1.3, round = 1
    2: x = 2.6, round = 3
    3: x = 3.9, round = 4
    4: x = 5.2, round = 5
    5: x = 6.5, round = 7
    6: x = 7.8, round = 8 etc

    As you see, I DONT throw away the floating point, I keep it in to keep the precision of the base arithmetic, but just loose the precision when I render to the screen.

    AND back to the original Q
    If I WAS to convert from float to int, what is the fastest way?

    Surely if the only answers are 'don't use floats' then why would compilers even support them?

  6. #21
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Surely if the only answers are 'don't use floats' then why would compilers even support them?
    For easy of use. Similarly, if you asked "how could I instruct the CPU to run <this set> of instructions the fastest possible" you might ask "why do languages other than assembly exist? Or machine language even. The compiler has to give you the ability to use floating point numbers according to the C standard. But you asked how to do something specific with floats as fast as possible.
    If you understand what you're doing, you're not learning anything.

  7. #22
    Registered User
    Join Date
    Oct 2004
    Posts
    36
    But you asked how to do something specific with floats as fast as possible.
    My point exactly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM