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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:01:06+00:00 2026-05-27T14:01:06+00:00

I have a small Haskell program, and am curious why a divide by zero

  • 0

I have a small Haskell program, and am curious why a divide by zero exception gets thrown when I run it (GHC 7.0.3)

import qualified Data.ByteString.Lazy as B
import Codec.Utils

convert :: B.ByteString -> [Octet]
convert bs = map (head . toTwosComp) $ B.unpack bs

main = putStrLn $ show $ convert $ B.pack [1, 2, 3, 4]

Can anyone help me understand what’s going on here?

  • 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-27T14:01:07+00:00Added an answer on May 27, 2026 at 2:01 pm

    We can reduce this to

    GHCi> toTwosComp (1 :: Word8)
    *** Exception: divide by zero
    

    Note that this works if you use Word16, Int, Integer, or any number of types, but fails when using Word8, as B.unpack gives us! So why does it fail? The answer is found in the source code to Codec.Utils.toTwosComp. You can see that it calls toBase 256 (abs x), where x is the argument.

    The type of toBase — not exported from the Codec.Utils module, and without an explicit type signature in the source, but you can see this by putting the definition in a file and asking GHCi what the type is (:t toBase), is

    toBase :: (Integral a, Num b) => a -> a -> [b]
    

    So, annotating types explicitly, toTwosComp is calling toBase (256 :: Word8) (abs x :: Word8). What’s 256 :: Word8?

    GHCi> 256 :: Word8
    0
    

    Oops! 256 > 255, so we can’t hold it in a Word8, and it overflows silently. toBase, in the process of its base conversion, divides by the base being used, so it ends up dividing by zero, producing the behaviour you’re getting.

    What’s the solution? Convert the Word8s to Ints with fromIntegral before passing them to toTwosComp:

    convert :: B.ByteString -> [Octet]
    convert = map convert' . B.unpack
      where convert' b = head $ toTwosComp (fromIntegral b :: Int)
    

    Personally, this behaviour worries me a bit, and I think toTwosComp should probably do such a conversion itself, likely to Integer, so that it works with integral types of every size; but this would incur a performance penalty that the developers might not like the idea of. Still, this is a pretty confusing failure that requires source-diving to understand. Thankfully, it’s very easy to work around.

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

Sidebar

Related Questions

I've written a small Haskell program to print the MD5 checksums of all files
I'm developing a small haskell program that uses an external static library I've developed
i have small problem i.e. their is one celltable with lot of data it
I have a problem. I wrote a big Haskell program, and it always works
I'm writing a small program with IO actions in Haskell here is module StackQuestion
I have a problem compiling Haskell programs, that import the Text.Regex.Posix module. I have
I wrote a small program, kind of specialized HTTP server in haskell, which is
I have small COCOA Mac OS application to play with core data. I have
have small problem, and would very much appreciate help :) I should convert byte
I have small page which has label, DropDownList and a submit button. <div> <asp:label

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.