# C float comparison based on "extended precision" floats fails unexpectedly

**URL:** <https://swi-prolog.discourse.group/t/c-float-comparison-based-on-extended-precision-floats-fails-unexpectedly/6031>\
**Category:** Help!\
**Created:** [December 18, 2022, 5:19pm UTC](https://swi-prolog.discourse.group/t/c-float-comparison-based-on-extended-precision-floats-fails-unexpectedly/6031 "2022-12-18T17:19:51Z")\
**Posts on this page:** 1\
**Page:** 2

<div class="post-metadata">

**Author:** ![ridgeworks](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ridgeworks/32/886_2.png) [@ridgeworks](https://swi-prolog.discourse.group/u/ridgeworks)\
**Post date:** [January 4, 2023, 4:54pm UTC](https://swi-prolog.discourse.group/t/c-float-comparison-based-on-extended-precision-floats-fails-unexpectedly/6031/38 "2023-01-04T16:54:38Z")

</div>

> [@anon95304481](#):
>
> Now you can spare a further mpq\_canonicalize(). Namely here,  
> I guess [rational/1](https://www.swi-prolog.org/pldoc/doc_for?object=rational/1) will call mpq\_canonicalize(), right?

Almost certainly, but that detail is buried in the GMP function `mpq_set_d` which sets a rational fraction from a floating point value. It’s called from both the `rational` arithmetic function and the new C implementation of `rationalize`.

There may be faster ways of creating the starting “rational” (not necessarily canonicalized) but I can’t think of any easier, so I’m inclined to leave it until some new use case requires an incremental improvement. (There’s now a PR for the new implementation so the whole source is available for review.)

[Previous page](https://swi-prolog.discourse.group/t/c-float-comparison-based-on-extended-precision-floats-fails-unexpectedly/6031.md?page=1)
