1 Replies - 599 Views - Last Post: 23 October 2016 - 11:52 AM

#1 notebook123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-October 16

ML sum unification error

Posted 23 October 2016 - 11:40 AM

Hi,

I am trying to carry out approximations for e:
function eapprox(n) = 1+11!+12!+⋯+1(n−1)!, in ML.

My code looks like this:

[ fun facr(0) = 1
| facr(n) = n * facr(n-1) ;

fun sumr(x:real, n)=
if n=1 then x
else x + sumr(x/real(facr(n)) , n-1);

fun eapprox(n:real) = suma(1.0 , n); ]

I get an error unifying int and real, but I don't know how to solve this in ways other than I have tried in my code.

Many thanks for your help

View Postnotebook123, on 23 October 2016 - 11:38 AM, said:

Hi,

I am trying to carry out approximations for e:
function eapprox(n) = 1+11!+12!+⋯+1(n−1)!, in ML.

My code looks like this:

[ fun facr(0) = 1
| facr(n) = n * facr(n-1) ;

fun sumr(x:real, n)=
if n=1 then x
else x + sumr(x/real(facr(n)) , n-1);

fun eapprox(n:real) = suma(1.0 , n); ]

I get an error unifying int and real, but I don't know how to solve this in ways other than I have tried in my code.

Many thanks for your help



sorry, should say 1+1/1!+1/2!+⋯+1/(n−1)!,

Is This A Good Question/Topic? 0
  • +

Replies To: ML sum unification error

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2517
  • View blog
  • Posts: 4,001
  • Joined: 21-June 11

Re: ML sum unification error

Posted 23 October 2016 - 11:52 AM

In the future please post the complete error message, including the location info (i.e. where in the code the error happens).

It seems like your problem is that you've declared n to be an int in the definition of eapprox, but you otherwise treat it as an int: you subtract 1 from it, not 1.0, you compare it to 1 (in fact you use = on it at all) and you pass it as an argument to facr, which takes an int - though given the name, that's kind of confusing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1