POV-Ray : Newsgroups : povray.newusers : Using conditional expression in functions. How to write step functions? Server Time
1 Nov 2024 08:11:42 EDT (-0400)
  Using conditional expression in functions. How to write step functions? (Message 1 to 9 of 9)  
From: Evgenij Gagauz
Subject: Using conditional expression in functions. How to write step functions?
Date: 18 Dec 2009 11:25:01
Message: <web.4b2babbe34012ef28bf23f70@news.povray.org>
Is there a way to use an conditional expression in function?

For example, how can I write something like that (the code isn't compilable):
#local  =
function(a) {
  #if (a < .1)
    1
  #else
    0
  #end
}

The compiler says that it doesn't know anything about "a".

Ok, the "#if" condition is calculated only once, but function will be calculated
many times. I try to rewrite the function:

#local  =
function(a) {
  (a < .1) ? 1 : 0
}

But the compiler says that "Expected 'operator', ? found instead". But as I know
the question sign is a float operator.

Where I'm wrong? And how to write for example step functions? Shall I use the
"select" function?
Unfortunatly I can't see a good way for modelling function like this:
___----_-__- with the "select" function.


Post a reply to this message

From: Warp
Subject: Re: Using conditional expression in functions. How to write step functions?
Date: 18 Dec 2009 11:41:06
Message: <4b2bb0a2@news.povray.org>
Evgenij Gagauz <nomail@nomail> wrote:
> Is there a way to use an conditional expression in function?

> For example, how can I write something like that (the code isn't compilable):
> #local  =
> function(a) {
>   #if (a < .1)
>     1
>   #else
>     0
>   #end
> }

> The compiler says that it doesn't know anything about "a".

  Because 'a' is not a declared identifier. (Function parameters have nothing
to do with declared identifiers and are invisible to any # commands.)

> Ok, the "#if" condition is calculated only once, but function will be calculated
> many times. I try to rewrite the function:

> #local  =
> function(a) {
>   (a < .1) ? 1 : 0
> }

  The ?: syntax is not supported by user-defined functions.

> Shall I use the "select" function?

  That's what it was created for.

> Unfortunatly I can't see a good way for modelling function like this:
> ___----_-__- with the "select" function.

  If you can express it with ?:, you can express it with select().

-- 
                                                          - Warp


Post a reply to this message

From: Evgenij Gagauz
Subject: Re: Using conditional expression in functions. How to write step functions?
Date: 18 Dec 2009 14:05:01
Message: <web.4b2bd247be6ce51d8bf23f70@news.povray.org>
Thanks for a fast answer. =)

>   If you can express it with ?:, you can express it with select().

Unfortunatly the best way is express it with the something like case statement,
not with the "select" function.

I think that SDL will contain constructions for situations like this. May be a
feature or a feature request is exists in the beta version?


Post a reply to this message

From: Warp
Subject: Re: Using conditional expression in functions. How to write step functions?
Date: 18 Dec 2009 15:24:06
Message: <4b2be4e6@news.povray.org>
Evgenij Gagauz <nomail@nomail> wrote:
> Thanks for a fast answer. =)

> >   If you can express it with ?:, you can express it with select().

> Unfortunatly the best way is express it with the something like case statement,
> not with the "select" function.

  But it's not like it's *impossible* to express the same thing with select().

-- 
                                                          - Warp


Post a reply to this message

From: Christian Froeschlin
Subject: Re: Using conditional expression in functions. How to write step functions?
Date: 18 Dec 2009 18:06:40
Message: <4b2c0b00$1@news.povray.org>
Evgenij Gagauz wrote:

> Unfortunatly the best way is express it with the something like case statement,
> not with the "select" function.

I just hacked something up which might do what you want,
with the following limitations

1. No nesting of fswitch blocks
2. Quotes required around all subexpressions (except plain numbers)

Basically, it is a set of macros which will write an include
file containing the nested select statements.

#macro fswitch(CONTROL)
#declare CASE_CONTROL = CONTROL;
#declare CASE_COUNTER = 0;
#fopen   CASE_FILE "switch_tmp.inc" write
#end

#macro fcase(CASE,VALUE)
#declare CASE_COUNTER = CASE_COUNTER + 1;
#write(CASE_FILE,"select((",CASE_CONTROL,")-(",CASE,"),",VALUE,",\n")
#end

#macro fdefault(VALUE)
#write(CASE_FILE,VALUE)
#while(CASE_COUNTER > 0)
   #write(CASE_FILE,")")
   #declare CASE_COUNTER = CASE_COUNTER - 1;
#end
#fclose CASE_FILE
#include "switch_tmp.inc"
#end

function(a,b)
{
   fswitch("a")
   fcase(0.1,"1")
   fcase(0.5,"b")
   fdefault("0")
}


Post a reply to this message

From: Christian Froeschlin
Subject: Re: Using conditional expression in functions. How to write step functions?
Date: 18 Dec 2009 18:07:10
Message: <4b2c0b1e$1@news.povray.org>
Warp wrote:

>   But it's not like it's *impossible* to express the same thing with select().

Unless you run into some sort of nesting limit, maybe ;)


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: Using conditional expression in functions. How to write step functions?
Date: 18 Dec 2009 19:10:00
Message: <web.4b2c1926be6ce51da049a19e0@news.povray.org>
"Evgenij Gagauz" <nomail@nomail> wrote:
> Is there a way to use an conditional expression in function?
>
> For example, how can I write something like that (the code isn't compilable):
> #local  =
> function(a) {
>   #if (a < .1)
>     1
>   #else
>     0
>   #end
> }

#declare StepFn = function(A) { select(A - 0.1, 1, 0) }

--
Tor Olav
http://subcube.com


Post a reply to this message

From: Mike Williams
Subject: Re: Using conditional expression in functions. How to write step functions?
Date: 19 Dec 2009 03:06:00
Message: <EvyzVLOflILLFwtm@econym.demon.co.uk>
Wasn't it Evgenij Gagauz who wrote:
>I can't see a good way for modelling function like this:
>___----_-__- with the "select" function.

That looks something like what I do here:

http://www.econym.demon.co.uk/isotut/arrays.htm

-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

From: Evgenij Gagauz
Subject: Re: Using conditional expression in functions. How to write step functions?
Date: 19 Dec 2009 11:10:01
Message: <web.4b2cfa77be6ce51d8bf23f70@news.povray.org>
There are interesting macroses fswitch, fcase and fdefault. It is to be
regretted that those macroses can not work without an additional file.


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.