Parsing of floating point numbers with error on truncated precision

I am writing a parser for a LIN Description File(LDF). In a LDF file there may be floats. Currently I have a lexer that produces following question-relevant tokens:

  • Number: any character sequence composed of 0-9 digits stored in a ReadOnlySequence(Leading and trailing zeroes are preserved)
  • Dot: represents a ‘.’ character without a value

Now whenever a floating point number is expected one of following sequences of tokens is expected:

  • Number -> Dot -> Number
  • Number

Now with the information I can get from these tokens I could create a Span of chars that has the digits of both Number tokens separated with a ‘.’ or simply a Span of chars that has the digits of the Number. I can then call double.TryParse with the newly created Span.

Now I am concerned about the precision truncating nature of double.TryParse. It will return Infinity if the parsed number is to large for a double and I can check it and act accordingly. However, what I would like to do is somehow detect if the parsed number can’t be represented “precisely enough”(truncating) and then inform the user that a floating point number of this precision is not supported and preferably fail this parsing instance.

What I mean by precisely enough is:

  • 0.1 can’t be precisely represented as a double or float for that matter. But it gets close enough. The loss of precision happens because this fraction is recurring in base 2. This should be allowed because that’s the best we can do with any IEEE-754 number
  • 1.123456789123456789 would result in 1.1234567891234568 where the last two digits are lost. I had hoped that the TryParse function method would fail in this case since the input is two precise. This is not the case. I am looking for a workaround where I can detect a loss of precision caused by an invalid value rather than because of limitation of floating point numbers themselves.

Is there a way to detect such an overflow in significant digits? I need to do this without any managed heap allocations.

The rationale behind this is that whoever wrote the LDF file meant something by including this many significant digits. If some of those digits were to be truncated this might cause the program to not behave correctly based on the input file. This is why I want to consider this an error. Potentially I could limit the amount if decimal digits(Sum of ReadOnlySequence lengths) to something that would definitely not overflow but then very small numbers with many 0s before significant digits would be false positives. I hope someone can share some insights into this.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật