Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8403397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:19:57+00:00 2026-06-09T22:19:57+00:00

I often need to floor or ceil a CGFloat to an int , for

  • 0

I often need to floor or ceil a CGFloat to an int, for calculation of an array index.

The problem I permanently see with floorf(theCGFloat) or ceilf(theCGFloat) is that there can be troubles with floating point inaccuracies.

So what if my CGFloat is 2.0f but internally it is represented as 1.999999999999f or something like that. I do floorf and get 1.0f, which is a float again. And yet I must cast this beast to int which may introduce another problem.

Is there a best practice how to floor or ceil a float to an int such that something like 2.0 would never accidentally get floored to 1 and something like 2.0 would never accidentally get ceiled to 2?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:19:59+00:00Added an answer on June 9, 2026 at 10:19 pm

    There are a couple misconceptions in your question.

    what if my CGFloat is 2.0f but internally it is represented as 1.999999999999f

    can’t happen; 2.0, like all reasonably small integers, has an exact representation in floating-point. If your CGFloat is 2.0f, then it really is 2.0.

    something like 2.0 would never accidentally get ceiled to 2

    The ceiling of 2.0 is 2; what else would it possibly be?


    I think the question that you’re really asking is “suppose I do a calculation that produces an inexact result, which mathematically should be exactly 2.0, but is actually slightly less; when I apply floor to that value, I get 1.0 instead of 2.0–how do I prevent this?”

    That’s actually a fairly subtle question that doesn’t have a single “right” answer. How have you computed the input value? What are you going to do with the result?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While debugging jQuery apps that use AJAX, I often have the need to see
The problem: I often need to select a set of variables from a data.frame
When working with Project Euler problems I often need large (> 10**7) bit array's.
The Problem : Object models built using an ORM often need to perform multiple
Problem: I often need to refer a row to a column (using =Cell). When
In php, I often need to map a variable using an array ... but
I often need to use a function which performs and action X is condition
I often need relatively small (<10000 entries <1kb) caches for speeding up calculations. My
I often need to list items with various-sized images on the left and text
I often need to fetch tgz files, decompress them, and then delete the tgz.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.