MVMString * MVM_coerce_n_s(MVMThreadContext *tc, MVMnum64 n) { if (n == MVM_num_posinf(tc)) { return MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "Inf"); } else if (n == MVM_num_neginf(tc)) { return MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "-Inf"); } else if (n != n) { return MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "NaN"); } else { char buf[64]; int i; if (snprintf(buf, 64, "%.15g", n) < 0) MVM_exception_throw_adhoc(tc, "Could not stringify number"); if (strstr(buf, ".")) { MVMint64 is_not_scientific = !strstr(buf, "e"); i = strlen(buf); while (i > 1 && ((buf[--i] == '0' && is_not_scientific) || buf[i] == ' ')) buf[i] = '\0'; if (buf[i] == '.') buf[i] = '\0'; } return MVM_string_ascii_decode(tc, tc->instance->VMString, buf, strlen(buf)); } }
MVMnum64 MVM_coerce_s_n(MVMThreadContext *tc, MVMString *s) { char *enc = MVM_string_ascii_encode(tc, s, NULL); MVMnum64 n; if (strcmp(enc, "NaN") == 0) n = MVM_num_nan(tc); else if (strcmp(enc, "Inf") == 0) n = MVM_num_posinf(tc); else if (strcmp(enc, "+Inf") == 0) n = MVM_num_posinf(tc); else if (strcmp(enc, "-Inf") == 0) n = MVM_num_neginf(tc); else n = atof(enc); MVM_free(enc); return n; }