Write your own implementation of math's floor function, C - Stack Overflow
Does anyone know how to create a floor function in c
Functions in SQL
If you have the ability to create a table, that would be a much better way to go. A permanent or temporary table will work, but permanent is preferred (so that you don't have recreate & refill the table every time, and others can see/use your table).
The table would look something like this:
0 19 '< 20' 20 29 '20-29' 30 39 '30-39' 40 49 '40-49' 50 59 '50-59' 60 255 '60+'If the buckets could potentially shift over time, and you need to maintain historical accuracy, you can even add two more columns for the start and end date.
Here's how to create and fill the table (I don't know what flavor of SQL you're using, so I'll write it in SQL Server. There may be some small syntax differences):
create table dbo.Age_Buckets (
Min_Age tinyint not null
, Max_Age tinyint not null
, Age_Bucket varchar(10) not null
, primary key (Min_Age, Max_Age)
);
insert into
dbo.Age_Buckets
values
(0, 19, '<20')
, (20, 29, '20-29')
, (30, 39, '30-39')
, (40, 49, '40-49')
, (50, 59, '50-59')
, (60, 255, '60+')
;And here's how you'd use the table in your query:
select
ad.ID -- Whatever you need from this table
, ad.Age
, ab.Age_Bucket
from
Age_Data as ad
inner join
Age_Buckets as ab
on ad.Age between ab.Min_Age and ab.Max_Age
; More on reddit.com The complete solution for floor(nx+y) computation
Videos
Both of your attempts have limitations:
- If the
doublevalue is outside the range of theinttype, converting tointis implementation defined. - If the
doublevalue is negative but integral, returning(int)num - 1is incorrect.
Here is an (almost) portable version that tries to handle all cases:
double my_floor_2(double num) {
if (num >= LLONG_MAX || num <= LLONG_MIN || num != num) {
/* handle large values, infinities and nan */
return num;
}
long long n = (long long)num;
double d = (double)n;
if (d == num || num >= 0)
return d;
else
return d - 1;
}
It should be correct if type long long has more value bits than type double, which is the case on most modern systems.
No, you can't tackle it this way. The best way of writing your own implementation is to take the one from the C Standard Library on your platform. But note that might contain platform specific nuances so might not be portable.
The C Standard Library floor function is typically clever in that it doesn't work by taking a conversion to an integral type. If it did then you'd run the risk of signed integer overflow, the behaviour of which is undefined. (Note that the smallest possible range for an int is -32767 to +32767).
The precise implementation is also dependent on the floating point scheme used on your platform.
For a platform using IEEE754 floating point, and a long long type you could adopt this scheme:
- If the magnitude of the number is greater than 253, return it (as it's already integral).
- Else, cast to a 64-bit type (
long long), and return it back.
I'm working on a project that doesn't use any c libraries and can't figure it out