## Nervous ticks in C - a reminder of the cruel logic of logic [Software]

Hi all,

sometimes logic totally sucks.

I am making code in C for plotting various graphs, and am making a little routine for inserting ticks on an axis. Ticks have to be "nice" (yes, that's also the word used in the R documentation).

So, let us say the maximum observed value is

You can see one definition of the ceil function here.

So, one way to test it is like this, which will print the actual ticks:

When I call that function with arguments

but it doesn't print 0.25 ?!???

If I add a line like

then I get

This is the beauty of C. Sometimes it is all-out warfare with this language and little tasks that should take 2 minutes take 2 days and end up with theory around little endians and data types and ANSI specifications and God knows what else .

Edit: Category changed; see also this post #1. [Helmut]

sometimes logic totally sucks.

I am making code in C for plotting various graphs, and am making a little routine for inserting ticks on an axis. Ticks have to be "nice" (yes, that's also the word used in the R documentation).

So, let us say the maximum observed value is

`x=0.234567`

and I want ticks evenly dispersed at a distance of dx=0.05 (say from 0.0 and upwards). So, I thought I was clever and calculated the "maximum" tick as `xmax = dx*ceil(x/dx)`

You can see one definition of the ceil function here.

So, one way to test it is like this, which will print the actual ticks:

```
```

int Test10001(double x, double dx)

{

double xmax;

xmax=dx*ceil(x/dx);

x=0.0;

while (x<=xmax)

{

printf("x=%f\n",x);

x=x+dx;

}

return(0);

}

When I call that function with arguments

`0.234567 `

and `0.05`

it prints:`x=0.000000`

x=0.050000

x=0.100000

x=0.150000

x=0.200000

but it doesn't print 0.25 ?!???

If I add a line like

`printf("xmax=%f\n", xmax);`

then I get

`xmax=0.250000`

This is the beauty of C. Sometimes it is all-out warfare with this language and little tasks that should take 2 minutes take 2 days and end up with theory around little endians and data types and ANSI specifications and God knows what else .

Edit: Category changed; see also this post #1. [Helmut]

—

Pass or fail!

ElMaestro

Pass or fail!

ElMaestro

### Complete thread:

- Nervous ticks in C - a reminder of the cruel logic of logicElMaestro 2018-03-15 10:24 [Software]
- Nervous ticks in C - a reminder of the cruel logic of logic mittyri 2018-03-15 11:55
- Binary representation ElMaestro 2018-03-16 13:13
- bypassing eqaulity conditions mittyri 2018-03-17 18:12

- Binary representation ElMaestro 2018-03-16 13:13
- Nervous ticks in C - a reminder of the cruel logic of logic nobody 2018-03-18 13:13
- Nervous ticks in C - a reminder of the cruel logic of logic ElMaestro 2018-03-19 09:44
- Nervous ticks in C - a reminder of the cruel logic of logic nobody 2018-03-22 08:56
- Check it out for yourself - Dissolution Bootstrap:-) ElMaestro 2018-03-22 17:16
- Check it out for yourself - Dissolution Bootstrap:-) Helmut 2018-03-22 18:50
- Check it out for yourself - Dissolution Bootstrap:-) ElMaestro 2018-03-22 19:04
- Check it out for yourself - Dissolution Bootstrap:-) Helmut 2018-03-22 22:22

- Check it out for yourself - Dissolution Bootstrap:-) ElMaestro 2018-03-22 19:04

- Check it out for yourself - Dissolution Bootstrap:-) Helmut 2018-03-22 18:50

- Check it out for yourself - Dissolution Bootstrap:-) ElMaestro 2018-03-22 17:16

- Nervous ticks in C - a reminder of the cruel logic of logic nobody 2018-03-22 08:56

- Nervous ticks in C - a reminder of the cruel logic of logic ElMaestro 2018-03-19 09:44

- Nervous ticks in C - a reminder of the cruel logic of logic mittyri 2018-03-15 11:55