1. help?

I'm sorry, I'm probably so dense today.
I can't see what's wrong in this little piece of code:

include std/math.e 
 
function minmax(atom min, atom max, atom val) 
    atom temp = max ({min, val}) 
    atom res = min({max, temp}) 
    return res 
end function 
 
? minmax(7.2,99,6) 

thanks in advance, kobi

new topic     » topic index » view message » categorize

2. Re: help?

kobi said...

I'm sorry, I'm probably so dense today.
I can't see what's wrong in this little piece of code:

I don't know what you are trying to achieve so I can't tell what the correct result should be.

new topic     » goto parent     » topic index » view message » categorize

3. Re: help?

Are you trying to achieve the same effect as ensure_in_range?

new topic     » goto parent     » topic index » view message » categorize

4. Re: help?

kobi said...

I'm sorry, I'm probably so dense today.
I can't see what's wrong in this little piece of code:

include std/math.e 
 
function minmax(atom min, atom max, atom val) 
    atom temp = max ({min, val}) 
    atom res = min({max, temp}) 
    return res 
end function 
 
? minmax(7.2,99,6) 

thanks in advance, kobi

besides reusing the max name? surprisingly legal in euphoria more often than not, but always confusing. another reason is to avoid catching euphoria on a bad day with an edge case. try something else more direct if it looks like a bug.

new topic     » goto parent     » topic index » view message » categorize

5. Re: help?

kobi said...

I'm sorry, I'm probably so dense today.
I can't see what's wrong in this little piece of code:

function minmax(atom min, atom max, atom val)  
    atom temp = math:max ({min, val})  
    atom res = math:min({max, temp})  
    return res  
end function  

qualifying w/math: gets rid of the syntax error and produces 7.2 wrong again?

seems it should test min first otherwise should be called maxmin

probably a bug.

new topic     » goto parent     » topic index » view message » categorize

6. Re: help?

Thanks for all the replies!
I got a syntax error and
didn't see that the variable name is the same as the function's name.

new topic     » goto parent     » topic index » view message » categorize

7. Re: help?

kobi said...

I can't see what's wrong in this little piece of code:

include std/math.e 
 
function minmax(atom min, atom max, atom val) 
    atom temp = max ({min, val}) 
    atom res = min({max, temp}) 
    return res 
end function 
 
? minmax(7.2,99,6) 

Although your original problem has been resolved, you might like to consider a more straight forward version ...

function minmax(atom pMin, atom pMax, atom pVal)  
    if pVal > pMax then 
        return pMax 
    end if 
 
    if pVal < pMin then 
        return pMin 
    end if 
 
    return pVal 
end function  
  
for i = 5 to 120 by 20 do 
	? {i, minmax(7.2,99,i) } 
end for 

Also note that I've used my personal naming convention, that helps avoid name clashes.

  • Parameters are prefixed with 'p'
  • Variables local to a routine are prefixed with 'l'
  • Variables local to a module (file) are prefixed with 'm'
  • Variables that are exposed to other files are prefixed with 'g'
  • Constants local to a file are prefixed with 'k'
  • Constants that are exposed to other files are (usually) in all uppercase.
new topic     » goto parent     » topic index » view message » categorize

8. Re: help?

DerekParnell said...
function minmax(atom pMin, atom pMax, atom pVal)  
    if pVal > pMax then 
        return pMax 
    end if 
 
    if pVal < pMin then 
        return pMin 
    end if 
 
    return pVal 
end function  
  
for i = 5 to 120 by 20 do 
	? {i, minmax(7.2,99,i) } 
end for 

ah, yes, much easier to understand.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu