c# - smallest float that can be summed to another float -
assuming there's float number 7 digits how find smallest number can added float?
example 1: 1234.567f + 0000.001f = 1234.568f
example 2: 0.01234567 + 0.00000001f = 0.01234568f
op added c# after posting of c answer.
will leave c solution reference.
print number out in exponential format , change each digit, '0' or '1'
float smallest_float_summable(float f, int digs) { char buf[20]; sprintf(buf, "%.*e", digs - 1, f); char *p = buf; while (*p != 'e') { if (isdigit(*p)) { *p = '0' + (p[1] == 'e'); } p++; } float y; sscanf(buf, "%e", &y); return y; } printf("%e\n", smallest_float_summable(1234.567f, 7)); // 1.000000e-03 this not smallest typically number near 1/2 value of smallest_float_summable() effect change, yet seems match op's intent.
to smallest number effect change, simple use nextafter()
the
nextafterfunctions determine next representable value, in type of function, afterxin direction ofy... c11dr §7.12.11.3 2
#include <math.h> float smallest_change(float f) { float dif = f - nextafterf(f, 0); return dif; } [edit]
@aka.nice correctly points out smaller, maybe 1/2 dif effect change. ponder this.
Comments
Post a Comment