Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Curious about rounding #50

Open
JPHutchins opened this issue Jan 3, 2024 · 2 comments
Open

Curious about rounding #50

JPHutchins opened this issue Jan 3, 2024 · 2 comments

Comments

@JPHutchins
Copy link
Contributor

Often, my use case may be to avoid soft floats on hardware that has no FPU. See this section of code:

static inline fix16_t fix16_from_float(float a)
{
float temp = a * fix16_one;
#ifndef FIXMATH_NO_ROUNDING
temp += (temp >= 0) ? 0.5f : -0.5f;
#endif
return (fix16_t)temp;
}
static inline fix16_t fix16_from_dbl(double a)
{
double temp = a * fix16_one;
/* F16() and F16C() are both rounding allways, so this should as well */
//#ifndef FIXMATH_NO_ROUNDING
temp += (double)((temp >= 0) ? 0.5f : -0.5f);
//#endif
return (fix16_t)temp;
}

First, I think it's problematic that "from double" is disregarding the rounding compile def while "from float" respects it.

Second, I'm curious about the statement:

temp += (temp >= 0) ? 0.5f : -0.5f;

In my use case, won't this bring in the soft fadd that I don't want? Then again, I suppose that the user is just doing it wrong if they don't want any floats yet the end up using fix16_from_float at runtime!

@PetteriAimonen
Copy link
Owner

I agree that the rounding difference is weird and should be fixed. Not sure which way makes more sense though.

To avoid any float use, I think you'd use F16() or F16C() macro. Though probably in most cases compiler will optimize away the inline functions.

@JPHutchins
Copy link
Contributor Author

Totally agreed re: F16 and F16C.

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

No branches or pull requests

2 participants