# My saturation block made in c-script can't work

**URL:** <https://forum.plexim.com/t/my-saturation-block-made-in-c-script-cant-work/1130>\
**Category:** PLECS\
**Tags:** c-script\
**Created:** [June 28, 2022, 11:49pm UTC](https://forum.plexim.com/t/my-saturation-block-made-in-c-script-cant-work/1130 "2022-06-28T23:49:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fire](https://avatars.discourse-cdn.com/v4/letter/f/67e7ee/32.png) [@fire](https://forum.plexim.com/u/fire)\
**Post date:** [June 28, 2022, 11:49pm UTC](https://forum.plexim.com/t/my-saturation-block-made-in-c-script-cant-work/1130/1 "2022-06-28T23:49:02Z")

</div>

I made a saturation bolck with c-script, but it can’t work normally compared with the code in the help document, which is the inside code about saturation block in the library.

So can you tell me the difference about two c-scripts?

appreciation.

[saturation.plecs](https://forum.plexim.com/uploads/short-url/7GMmfkQQNLeIZigUphGbJdTJ3qD.plecs) (12.7 KB)

 ![help document.png](https://europe1.discourse-cdn.com/flex006/uploads/plecs/original/1X/81c79b304761de3ed74d49de8cd3bc3d3c798b73.png)

---

<div class="post-metadata">

**Author:** ![Oliver\_Schwartz](https://dub1.discourse-cdn.com/flex006/user_avatar/forum.plexim.com/oliver_schwartz/32/6_2.png) [@Oliver\_Schwartz](https://forum.plexim.com/u/Oliver_Schwartz)\
**Post date:** [June 30, 2022, 5:35am UTC](https://forum.plexim.com/t/my-saturation-block-made-in-c-script-cant-work/1130/2 "2022-06-30T05:35:46Z")

</div>

Your implementation does not behave correctly in minor timesteps. This is not obvious in the test model that you have attached, because the scope will only evaluate the output in major timesteps.

The difference becomes obvious if you feed the output of the C-Scripts to an integrator. With your implementation there is an integration error (the output of the integrator does not become 0 after one period), whereas the reference implementation works correctly (see attached model).

[saturation\_os.plecs](https://forum.plexim.com/uploads/short-url/twjoTN2RET13K4Y1BYVrREKJVMQ.plecs) (13.3 KB)

---

<div class="post-metadata">

**Author:** ![fire](https://avatars.discourse-cdn.com/v4/letter/f/67e7ee/32.png) [@fire](https://forum.plexim.com/u/fire)\
**Post date:** [July 4, 2022, 10:50am UTC](https://forum.plexim.com/t/my-saturation-block-made-in-c-script-cant-work/1130/3 "2022-07-04T10:50:38Z")

</div>

Thanks for you answer, but what I want to know is that what caused the difference? Is the using of the " if … switch…" ? after all, two c-scripts are so alike.

best wishes;

---

<div class="post-metadata">

**Author:** ![Oliver\_Schwartz](https://dub1.discourse-cdn.com/flex006/user_avatar/forum.plexim.com/oliver_schwartz/32/6_2.png) [@Oliver\_Schwartz](https://forum.plexim.com/u/Oliver_Schwartz)\
**Post date:** [July 4, 2022, 2:46pm UTC](https://forum.plexim.com/t/my-saturation-block-made-in-c-script-cant-work/1130/4 "2022-07-04T14:46:55Z")

</div>

They are not alike at all. The correct one specifies the behavior in major and minor steps, the other one only the behavior in major steps. The reason why the behavior in minor steps must be specified is given in the help document that you have cited.
