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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:47:09+00:00 2026-06-15T16:47:09+00:00

In playing with Scala’s combinator parsing framework, I came across the following problem when

  • 0

In playing with Scala’s combinator parsing framework, I came across the following problem when parsing floating point numbers:

import scala.util.parsing.combinator.JavaTokenParsers
import org.junit.runner.RunWith
import org.scalatest.FlatSpec
import org.scalatest.junit.ShouldMatchersForJUnit
import org.scalatest.junit.JUnitRunner

@RunWith(classOf[JUnitRunner])
class SandboxSpec extends FlatSpec with ShouldMatchersForJUnit {
  "A java token parser" should "parse a float" in {

    class Parser extends JavaTokenParsers {
      def realValue: Parser[Float] = floatingPointNumber ^^ {
        s => s.toFloat
      }
    }

    val p = new Parser()
    val result = p.parseAll(p.realValue, "5.4") match {
      case p.Success(x, _)     => x
      case p.Failure(msg, _) => fail(msg)
    }

    result should equal (5.4f plusOrMinus 0.0001f)

  }
}

This test produces the following error:

5.4 did not equal FloatTolerance(5.4,1.0E-4)

I’m not sure if it’s the parser code producing something not float-like (though, looking at it with the debugger, it’s clearly a Java Float), or if it’s an issue with the ScalaTest matcher.

Any thoughts?

  • 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-15T16:47:10+00:00Added an answer on June 15, 2026 at 4:47 pm

    In ScalaTest equal always means ==, thus object equality. Thus, your code does not succeed because 5.4 is not equal to an instance of FloatTolerance.

    To correct your test case use be, which is overloaded to take an instance of FloatTolerance:

    result should be (5.4f plusOrMinus 0.0001f)
    

    Btw, your code throws a warning:

    match may not be exhaustive. It would fail on the following input: Error(_, _)

    To eliminate it choose case p.NoSuccess(msg, _) => instead of case p.Failure(msg, _) =>

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

Sidebar

Related Questions

While playing around in Scala, I came up against something that I think should
I just started playing with scala and have been using the Scala By Example
I just started playing in scala. I got a method that accepts string array
I have been playing around with Scala parser combinators for some time now, and
I usually use .Net but now I'm playing Scala in IntelliJ 10.5.1. It's interesting
I am playing with Scala. What I need is just a brunch of function
I've just started playing around with Scala, and I just learned about how methods
While playing around with regexps in Scala I wrote something like this: scala> val
I'm playing around with using Scala for my processing projects. I've extended the Rect
I'm new to scala and just playing around with it in my free time

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.