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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:56:47+00:00 2026-05-27T05:56:47+00:00

As far as I know, exact comparison doesn’t make much sense with floating point

  • 0

As far as I know, exact comparison doesn’t make much sense with floating point values as what is intended to be 0.0001 can actually be something like 0.0001000…0001… Should I implement my own comparison function to specify precision or is there a common practice for this?

I used to use the following with C# (which, I suspect, is still wrong as a Double value can be probably uncapable of representing 0.0001 at all, even set as a constant (as Michael Borgwardt explained here)):

public static bool AlmostEquals(this double x, double y, double precision = 0.0001)
{
  if (precision < 0.0)
    throw new ArgumentException();

  return Math.Abs(x - y) <= precision;
}

Should I do something alike in Scala?

  • 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-05-27T05:56:48+00:00Added an answer on May 27, 2026 at 5:56 am

    Yes, you can do the same thing as in Java. You could also use some of Scala’s cool features and pimp the Double class with a ~= method that takes an implicit precision parameter that only needs to be specified once.

    scala> case class Precision(val p:Double)
    defined class Precision
    
    scala> class withAlmostEquals(d:Double) {
      def ~=(d2:Double)(implicit p:Precision) = (d-d2).abs <= p.p
    }
    defined class withAlmostEquals
    
    scala> implicit def add_~=(d:Double) = new withAlmostEquals(d)
    add_$tilde$eq: (d: Double)withAlmostEquals
    
    scala> 0.0~=0.0
    <console>:12: error: could not find implicit value for parameter p: Precision
                  0.0~=0.0
                     ^
    
    scala> implicit val precision = Precision(0.001)
    precision: Precision = Precision(0.001)
    
    scala> 0.0 ~= 0.00001
    res1: Boolean = true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As far as I know, in gcc you can write something like: #define DBGPRINT(fmt...)
As far as I know (not much I'll admit), the currently popular programming paradigms
As far as know, I must be careful with PHP, and I think Javascript.
As far as I know, foreign keys (FK) are used to aid the programmer
As far as I know, Flash has to pass info off to another external
As far as i know it is not possible to do the following in
As far as I know, there's no way to use {% include %} within
As far as i know, there is no direct equivalent in C#. My current
As far as I know, only the convenience methods return created objects with an
so far i only know to use Ctrl-Shift-A to do SVN in TextMate. is

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.