functionGemmaDouble function
Python's str(float). The template is Jinja, so every number in the prompt
was formatted by Python. Both languages print the shortest round-trip
digits, but they switch to exponent notation at different magnitudes:
Python below 1e-4 and from 1e16, Dart below 1e-6 and from 1e21.
Implementation
String functionGemmaDouble(double value) {
if (value.isNaN) return 'nan';
if (value.isInfinite) return value.isNegative ? '-inf' : 'inf';
final magnitude = value.abs();
if (magnitude == 0 || (magnitude >= 1e-4 && magnitude < 1e16)) {
return value.toString();
}
// Dart writes `1e-5`, Python pads the exponent to two digits: `1e-05`.
final exponential = value.toStringAsExponential();
final parts = RegExp(r'^(.*)e([+-])(\d+)$').firstMatch(exponential);
if (parts == null) return exponential;
return '${parts.group(1)}e${parts.group(2)}${parts.group(3)!.padLeft(2, '0')}';
}