Skip to content

Conversation

@GPaso
Copy link

@GPaso GPaso commented Feb 6, 2025

I found this library quite useful and I used some functions, but I also needed an extra one that i couldn't find so here it is.

I needed to divide a timespec by an integer value in order to have a mean value from a sum of timespecs.

I hope it's helpful and Thanks!

G.Pasotti

GPaso added 8 commits February 6, 2025 12:45
Needed to divide a timespec by an integer number
added timespec_div description
fixed some errors in TEST_DIV_FUNC
Fixes for 32 bits systems
Fixes for 32 bits
Rewrite the function to make it work for 32 bits
@GPaso
Copy link
Author

GPaso commented Feb 7, 2025

Sorry for the mess, i have some issues making it work for i386, do you have any suggestion to make it work without losing precision?

@solemnwarning
Copy link
Owner

Hi G.Pasotti,

Thanks for this, mostly looks good to me although I'd rename it to something like timespec_idiv() since there is already a timespec_mod() function which takes two timespec structures as operands rather than a timespec and an integer.

I haven't tested it, I think something like this might handle the rounding correctly:

unsigned long extra_nsec = rem_sec * NSEC_PER_SEC;
result.tv_nsec = (ts.tv_nsec + extra_nsec) / divisor;

I could also be completely wrong.

Daniel

changed name from timespec_div to timespec_idiv and test fix for fixing the rounding error
changed timespec_div to timespec_idiv
changed name from timespec_div to timespec_idiv
@GPaso
Copy link
Author

GPaso commented Feb 13, 2025

Your suggestion worked! Thanks!
Do you think i should add more test cases? or anything else?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants