haskell / bytestring

An efficient compact, immutable byte string type (both strict and lazy) suitable for binary or 8-bit character data.
http://hackage.haskell.org/package/bytestring
Other
291 stars 140 forks source link

RealFloat Zero Padded Exponent #643

Open BebeSparkelSparkel opened 10 months ago

BebeSparkelSparkel commented 10 months ago

Some dxf files have doubles encoded in scientific notation with the exponent padded with zeros.

https://github.com/LibreCAD/LibreCAD/blob/8e10245994d8307a76cedc3cbffe471de88c8325/librecad/support/library/algoritm/alg27.dxf#L1560

I suggest adding a Bool to FGeneric and FScientific to allow for padding the exponent with zeros to the maximum precision of the exponent.

clyring commented 10 months ago

Perhaps the way to support this is a higher-order interface that takes displayExponent :: Int -> Builder. Then you could write such a function that prints an exponent of 0 as E+000 or one that dislays it as e0. That seems a bit less nasty than having a bunch of flags for 'is e capitalized' and 'should a + be written for positive exponents' and 'should the exponent be zero-padded'.

Another idea is to provide a function of type Double -> (Builder, Int) where the Builder prints just the mantissa and the Int is the exponent.

BebeSparkelSparkel commented 10 months ago

This is much more flexible. Thank you for the idea.

BebeSparkelSparkel commented 10 months ago

@clyring I have been unable to make your suggestion performant. I have tried several ways but am consistently getting results that are 150-300% slower. Could you make some recommendations on how modify the overhaul-realfloat branch to implement your suggested change?

BebeSparkelSparkel commented 10 months ago

This seems to be going beyond the intended capabilities of the intended formatting capabilities.

clyring commented 10 months ago

@clyring I have been unable to make your suggestion performant. I have tried several ways but am consistently getting results that are 150-300% slower. Could you make some recommendations on how modify the overhaul-realfloat branch to implement your suggested change?

I'd expect there is a practical way to get an appropriately general interface for this without requiring such a performance hit. Perhaps using BoundedPrim instead of Builder helps? Or maybe we need to get the right amount of inlining to happen? But I don't have the spare focus to dig into this yet.

But it would be nice to provide an interface flexible enough to support this. Let's leave the issue open for now.

BebeSparkelSparkel commented 10 months ago

I have been thinking about this and perhaps the most flexible option for printing both the mantissa and exponent would be to extract F2S, D2S, and their dependencies that calculate the digits word and exponent integer to a new package. This would allow the most flexibility for custom printing of the mantissa and exponent and would allow other text types to use it for their own printing implementations.

BebeSparkelSparkel commented 10 months ago

Also, a problem with your proposed approach. FStandard does not use this so formatFloating cannot return (Builder, Int).