#4725

<stdfloat>

Openspinicistopened 7mo ago

Godbolt example: https://godbolt.org/z/rhd7vvT79

I'm having difficult with the types in , i.e. std::float16_t and std::float32_t. std::print can print a std::float16_t, but fmt::print cannot. Neither std::print or fmt::print can print a std::complex<std::float16_t>.

What is needed to format/print a std::complex<std::float16_t>? Is this something I can add a formatter for in my own code?

Thanks.

Activity

10
spinicist7mo ago

I attempted to provide a formatter for float16_t, but fmt::print doesn't find it https://godbolt.org/z/j39n8185W

Is this because float16_t lives in std?

sunmy20197mo ago

Is this because float16_t lives in std?

No. formatter only works for struct/class/union/enum/array.

What is needed to format/print a std::complexstd::float16_t? Is this something I can add a formatter for in my own code?

Yes. Just add a new formatter specialization.

sunmy20197mo ago

Fixing this requires an abi imcompatible change in fmt library. This will not available until next major release.

You can cast to f32 at now, or use a struct wrapper and implement formatter for it.

spinicist7mo ago

What needs to change - is it type_mapper?

Casting at the call site isn't really feasible throughout my code base, sadly.

vitaut7mo ago

What is needed to format/print a std::complexstd::float16_t? Is this something I can add a formatter for in my own code?

Normally you should only add formatters for code you control, so a supported way to do this is via a wrapper type in your namespace.

spinicist7mo ago

I'm happy to help with a PR to fix this properly but need some pointers on where to start. ABI isn't an issue for me. While it seems I'm the first person to try formatting a float16_t I can't imagine I'll be the last.

vitaut7mo ago

The FP implementation will soon be replaced with https://github.com/vitaut/zmij so the first step would be to add float16_t support there.

shreyas-s14037d ago

I’d like to work on this issue. I’ve looked into the formatting gap for std::float16_t/std::float32_t and std::complex<...>, and it appears to be related to missing formatter support rather than a bug in existing formatting behavior. If this is acceptable, I’d be happy to investigate and prepare a patch/PR for it. Please let me know if you’d like me to take this on.

spinicist7d ago

By all means, I have completely failed to get round to trying more myself.

vitaut7d ago

@shreyas-s1403 the FP formatter is about to be replaced so I wouldn't recommend building anything on top of existing facilities, see earlier discussion.