void softmax(double* input, size_t size) {
assert(0 <= size <= sizeof(input) / sizeof(double));
int i;
double m, sum, constant;
m = -INFINITY;
for (i = 0; i < size; ++i) {
if (m < input[i]) {
m = input[i];
sum = 0.0;
for (i = 0; i < size; ++i) {
sum += exp(input[i] - m);
constant = m + log(sum);
for (i = 0; i < size; ++i) {
input[i] = exp(input[i] - constant);
int main() {
double input[] = { 1. , 4.2 , 0.6 , 1.23 , 4.3 , 1.2, 2.5 };
int i, n = sizeof(input) / sizeof(double);
printf("Input Array: ");
for (i = 0; i < n; ++i)
printf("%lf ", input[i]);
printf("\n\n");
softmax(input, n);
printf("Softmax Array: ");
for (i = 0; i < n; ++i)
printf("%lf ", input[i]);
printf("\n\n");
Output
Input Array: 1.000000 4.200000 0.600000 1.230000 4.300000 1.200000 2.500000
Softmax Array: 0.016590 0.406995 0.011121 0.020880 0.449799 0.020263 0.07435