Search code examples
operatorsgnuplotmodulo

Is there a modulo operator for real variables in gnuplot?


So, gnuplot (I use 5.4 patchlevel 8) does not seem to support the modulo operator for real values:

gnuplot> a = 5.0 % 2.0
         non-integer operand for %

Is there any workaround over this limitation?


Solution

  • What about the following or are there other constraints?

    a = int(5.0) % int(2.0)
    

    Check help int.

    Addition:

    Ok, so this is the real reason of the question...

    Some time ago, I tried to plot a few periodic functions. Probably there are different ways using sin() or cos(), but then you are depending on the setting of set angle (radians|degrees).

    Below you are not dependent on the angle setting. I guess it should be self-explaining. A is amplitude, T is period, and t0 is time offset. If you plot these functions make sure that set samples is large enough to get "sharp" edges.

    Script:

    ### some special functions
    reset session
    
    set xrange[-2:2]
    set offsets 0.25,0.25,0.25,0.25
    set xzeroaxis
    set samples 500
    set key out
    set ytic 1
    
    tri(x,A,T,t0)  = A*(abs(floor((x-t0*T)/T-0.25)-(x-t0*T)/T+0.75)*4-1)    # triangle
    rect(x,A,T,t0) = A*sgn( floor((x-t0*T)/T)     -(x-t0*T)/T+0.50)         # rectangle
    saw(x,A,T,t0)  = A*   (-floor((x-t0*T)/T-0.50)+(x-t0*T)/T-1.00)*2       # saw tooth
    
    set multiplot layout 3,1
    
        plot tri(x,1,1,0) lw 2 lc "red" 
    
        plot rect(x,1,1,0) lw 2 lc "web-green"
    
        plot saw(x,1,1,0) lw 2 lc "web-blue"
    
    unset multiplot
    ### end of script
    

    Result:

    enter image description here