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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:34:50+00:00 2026-05-15T17:34:50+00:00

As part of a larger function definition, I needed to allow the domain (i,

  • 0

As part of a larger function definition, I needed to allow the domain (i, n) of a function to increment from i to n at varying rates. So I wrote:

f (i, n) k = [i, (i+k)..n]

into GHC. This returned odd results:

*Main> f (0.0, 1.0) 0.1
[0.0,0.1,0.2,0.30000000000000004,0.4000000000000001,0.5000000000000001,0.6000000000000001,0.7000000000000001,0.8,0.9,1.0]

Why does GHC return, e.g., 0.30000000000000004 instead of 0.3?

  • 1 1 Answer
  • 1 View
  • 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-15T17:34:51+00:00Added an answer on May 15, 2026 at 5:34 pm

    If i, n, and k are rational, you could go the infinite-precision route:

    f :: (Rational, Rational) -> Rational -> [Rational]
    f (i, n) k = [i, (i+k) .. n]
    

    The notation may require a bit of getting used to:

    ghci> f (0%1, 1%1) (1%10)
    [0 % 1,1 % 10,1 % 5,3 % 10,2 % 5,1 % 2,3 % 5,7 % 10,4 % 5,9 % 10,1 % 1]

    Think of the % as a funny looking fraction bar.

    You could view approximations with

    import Control.Monad (mapM_)
    import Data.Ratio (Rational, (%), denominator, numerator)
    import Text.Printf (printf)
    
    printApprox :: [Rational] -> IO ()
    printApprox rs = do
      mapM_ putRationalToOnePlaceLn rs
      where putRationalToOnePlaceLn :: Rational -> IO ()
            putRationalToOnePlaceLn r = do
              let toOnePlace :: String
                  toOnePlace = printf "%.1f" (numFrac / denomFrac)
                  numFrac, denomFrac :: Double
                  numFrac    = fromIntegral $ numerator   r
                  denomFrac  = fromIntegral $ denominator r
              putStrLn toOnePlace
    

    The code above is written in an imperative style with full type annotations. Read its type as transforming a list of rational numbers into some I/O action. The mapM_ combinator from Control.Monad evaluates an action (putRationalToOnePlaceLn in this case) for each value in a list (the rationals we want to approximate). You can think of it as sort of a for loop, and there is even a forM_ combinator that’s identical to mapM_ except the order of the arguments is reversed. The underscore at the end is a Haskell convention showing that it discards the results of running the actions, and note that there are mapM and forM that do collect those results.

    To arrange for the output of the approximations via putStrLn, we have to generate a string. If you were writing this in C, you’d have code along the lines of

    int numerator = 1, denominator = 10;
    printf("%.1f\n", (double) numerator / (double) denominator);
    

    The Haskell code above is similar in structure. The type of Haskell’s / operator is

    (/) :: (Fractional a) => a -> a -> a
    

    This says for some instance a of the typeclass Fractional, when given two values of the same type a, you’ll get back another value of that type.

    We can ask ghci to tell us about Fractional:

    ghci> :info Fractional
    class (Num a) => Fractional a where
      (/) :: a -> a -> a
      recip :: a -> a
      fromRational :: Rational -> a
        -- Defined in GHC.Real
    instance Fractional Float -- Defined in GHC.Float
    instance Fractional Double -- Defined in GHC.Float

    Notice the instance lines at the bottom. This means we can

    ghci> (22::Float) / (7::Float)
    3.142857

    or

    ghci> (22::Double) / (7::Double)
    3.142857142857143

    but not

    ghci> (22::Double) / (7::Float)
    
    <interactive>:1:16:
        Couldn't match expected type `Double' against inferred type `Float'
        In the second argument of `(/)', namely `(7 :: Float)'
        In the expression: (22 :: Double) / (7 :: Float)
        In the definition of `it': it = (22 :: Double) / (7 :: Float)

    and certainly not

    ghci> (22::Integer) / (7::Integer)
    
    <interactive>:1:0:
        No instance for (Fractional Integer)
          arising from a use of `/' at :1:0-27
        Possible fix: add an instance declaration for (Fractional Integer)
        In the expression: (22 :: Integer) / (7 :: Integer)
        In the definition of `it': it = (22 :: Integer) / (7 :: Integer)

    Remember that Haskell’s Rational type is defined as a ratio of Integers, so you can think of fromIntegral as sort of like a typecast in C.

    Even after reading A Gentle Introduction to Haskell: Numbers, you’ll still likely find Haskell to be frustratingly picky about mixing numeric types. It’s too easy for us, who perform infinite-precision arithmetic in our heads or on paper, to forget that computers have only finite precision and must deal in approximations. Type safety is a helpful reality check.

    Sample output:

    *Main> printApprox $ f (0%1, 1%1) (1%10)
    0.0
    0.1
    0.2
    0.3
    0.4
    0.5
    0.6
    0.7
    0.8
    0.9
    1.0

    The definition of printApprox probably seemed comforting with all the helpful signposts such as names of functions and parameters or type annotations. As you grow more experienced and comfortable with Haskell, such imperative-looking definitions will begin to look cluttered and messy.

    Haskell is a functional language: its strength is specifying the what, not the how, by assembling simple functions into more complex ones. Someone once suggested that Haskell manipulates functions as powerfully as Perl manipulates strings.

    In point-free style, the arguments disappear leaving the structure of the computation. Learning to read and this style does take practice, but you’ll find that it helps write cleaner code.

    With tweaks to the imports, we can define a point-free equivalent such as

    import Control.Arrow ((***), (&&&))
    import Control.Monad (join, mapM_)
    import Data.Ratio (Rational, (%), denominator, numerator)
    import Text.Printf (printf)
    
    printApproxPointFree :: [Rational] -> IO ()
    printApproxPointFree =
      mapM_       $
      putStrLn    .
      toOnePlace  .
      uncurry (/) .
      join (***) fromIntegral .
      (numerator &&& denominator)
      where toOnePlace = printf "%.1f" :: Double -> String
    

    We see a few familiar bits: our new friend mapM_, putStrLn, printf, numerator, and denominator.

    There’s also some weird stuff. Haskell’s $ operator is another way to write function application. Its definition is

    f $ x = f x
    

    It may not seem terribly useful until you try

    Prelude> show 1.0 / 2.0
    
    <interactive>:1:0:
        No instance for (Fractional String)
          arising from a use of `/' at :1:0-13
        Possible fix: add an instance declaration for (Fractional String)
        In the expression: show 1.0 / 2.0
        In the definition of `it': it = show 1.0 / 2.0

    You could write that line as

    show (1.0 / 2.0)
    

    or

    show $ 1.0 / 2.0
    

    So you can think of $ as another way to write parentheses.

    Then there’s . that means function composition. Its definition is

    (f . g) x = f (g x)
    

    which we could also write as

    (f . g) x = f $ g x
    

    As you can see, we apply the right-hand function and then feed the result to the left-hand function. You may remember definitions from mathematics textbooks such as

    rendered composition example

    The name . was chosen for its similarity in appearance to the raised dot.

    So with a chain of function compositions, it’s often easiest to understand it by reading back-to-front.

    The (numerator &&& denominator) bit uses a fan-out combinator from Control.Arrow. For example:

    ghci> (numerator &&& denominator) $ 1%3
    (1,3)

    So it applies two functions to the same value and gives you back a tuple with the results. Remember we need to apply fromIntegral to both the numerator and denominator, and that’s what join (***) fromIntegral does. Note that *** also comes from the Control.Arrow module.

    Finally, the / operator takes separate arguments, not a tuple. Thinking imperatively, you might want to write something like

    (fst tuple) / (snd tuple)
    

    where

    fst (a,_) = a
    snd (_,b) = b
    

    but think functionally! What if we could somehow transform / into a function that takes a tuple and uses its components as arguments for the division? That’s exactly what uncurry (/) does!

    You’ve taken a great first step with Haskell. Enjoy the journey!

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

Sidebar

Related Questions

I wrote a binary search function as part of a larger program, but it
It's a part of larger code base, which forces -Werror on gcc. This warning
I have a function that - as a larger part of a different program
Here is a part of a function I am trying for testing a larger
As part of a larger function, I'm writing some code to generate a vector/matrix
The segment below is part of a larger function in a larger file. I
This is part of a larger statement, but I'm wondering if CTE or another
I have the following in a program (part of a much larger function, but
As part of a larger web-app (using CakePHP), I'm putting together a simple blog
As part of a larger project I'm trying to implement a facility using JOGL

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.