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 8508809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:15:49+00:00 2026-06-11T03:15:49+00:00

I am writing a function in R that requires me to increment a counter

  • 0

I am writing a function in R that requires me to increment a counter by 0.05. When I condition on the value of the counter I am getting told that the value is not what it appears.

Example:

Set the counter to 0

cntr <- 0; cntr;

[1] 0

Increment counter by 0.05 and test

cntr <- cntr + 0.05; cntr;
[1] 0.05

> cntr == 0.05
[1] TRUE

So far, so good. But after a couple more iterations the following happens:

> cntr <- cntr + 0.05; cntr;
[1] 0.1
> cntr == 0.1
[1] TRUE
> cntr <- cntr + 0.05; cntr;
[1] 0.15
> cntr == 0.15
[1] FALSE

What is happening, and why? The value returned for cntr is 0.15 but it is not equal to this value?
Further investigation reveals this:

> cntr < 0.1500000000000001
[1] TRUE
> cntr < 0.15000000000000001
[1] FALSE

and

round(cntr, 2) == 0.15
[1] TRUE

Am I misunderstanding something, or is there something I am not seeing? Any help would be greatly appreciated. Thank you.

  • 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-11T03:15:51+00:00Added an answer on June 11, 2026 at 3:15 am

    You have to use isTRUE(all.equal(cntr, x)), where x is 0.1, 0.05, etc. instead. Your tests are failing because of floating point rounding errors. all.equal tests equality up to that error. Check out Circle 1 in R Inferno for other examples of where this might trip you up.

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

Sidebar

Related Questions

I am writing an app that requires the calculation of the Gamma function. A
I am writing a function that uses the minibuffer and requires a somewhat different
I'm writing a kernel module that requires function called in 0.1ms intervals measured with
I'm writing a function that requires input and my data validation looks very awkward.
I am writing function that involve other function from base R with a lot
I'm writing a function that replaces long hex coded color ( #334455 ) with
Suppose I'm writing a function that takes a list of integers and returns only
I am writing a function that takes in a output target file and a
I am writing a function that takes a string, string pointer and an int.
I am writing a Function that has a return-type which will dynamically change depending

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.