Find source "Ratio has a zero denominator" Exception

As a personal excercize in the process of learning Haskell , I am trying to transfer this F # snippet to Random Art .

I did not embed the full source code so as not to inflate the question, but is available as gist .

An important part of the program is this type Expr:


data Expr =
    VariableX
  | VariableY
  | Constant
  | Sum Expr Expr
  | Product Expr Expr
  | Mod Expr Expr
  | Well Expr
  | Tent Expr
  | Sin Expr
  | Level Expr Expr Expr
  | Mix Expr Expr Expr
  deriving Show

and two functions:

  • gen :: Int -> IO Expr random generates a tree structure, given the number of iterations

  • eval :: Expr -> IO (Point -> Rgb Double) scans the tree and completes the creation of the drawing function.

The higher the number, passed on gen, the higher the likelihood that will be created following exception: Ratio has zero denominator.

I am new to Haskell to solve the problem that I was trying to compile as described above:


ghc RandomArt.hs -prof -auto-all -caf-all

( ) info:


$ ./RandomArt +RTS -xc

*** Exception (reporting due to +RTS -xc): (THUNK_STATIC), stack trace:
  GHC.Real.CAF
  --> evaluated by: Main.eval.\,
  called from Main.eval,
  called from Main.tga.pxs',
  called from Main.tga,
  called from Main.save,
  called from Main.main,
  called from :Main.CAF:main
  --> evaluated by: Main.eval.\.r,
  called from Main.eval.\,
  called from Main.eval,
  called from Main.tga.pxs',
  called from Main.tga,
  called from Main.save,
  called from Main.main,
  called from :Main.CAF:main
*** Exception (reporting due to +RTS -xc): (THUNK_STATIC), stack trace:
  Main.tga,
  called from Main.save,
  called from Main.main,
  called from GHC.Real.CAF
RandomArt: Ratio has zero denominator

, TGA, , excercize ( OCaml).

Expr GHCi, , .

Haskell loch, , ( cabal install , ).

, , :

  • ( )?

  • , ( )?

.

+4
1

.

( )?

mod'. , Data.Fixed:

mod' :: RealFrac a => a -> a -> a
mod' _ 0 = error "Used mod' on 0"
mod' a b =
    let k = floor $ a / b
    in a - (fromInteger k) * b

Used mod' on 0.

, ( )?

:

Ratio has zero denominator

, , Ratio. , - . (/) mod', , :

  • (/) ±Infinity , Double,
  • mod' toRational , Ratio Integer.

, . , , b .

mod mod' b == 0 . , modulo :

prop_mod :: Integral n => n -> n -> Bool
prop_mod a b = 
  let m = a `mod` b
      d = a `div` b

  in a == b * d + m   -- (1)
     && abs m < abs b -- (2)

b == 0, (d, m), (1) (2). (2), mod . :

mod' :: RealFrac a => a -> a -> a
mod' a 0 = a -- this is arbitrary
mod' a b =
    let k = floor $ a / b
    in a - (fromInteger k) * b

. : " , mod ". F #, -, a % 0, .

mod, .

+5

Source: https://habr.com/ru/post/1608565/


All Articles