Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Comments on Show order equivalence between the rationals and the binary fractions

Post

Show order equivalence between the rationals and the binary fractions

+2
−0

Write a function $f$ from rational numbers to binary fractions (rational numbers whose denominator is a power of two) which is bijective and preserves order.

That means:

  • For every binary fraction $x$ there is exactly one rational $y$ such that $f(y)=x$.
  • $x < y \iff f(x) < f(y)$

That's all.

This is code-golf so the goal is to minimize the size of your source code as measured in bytes.

IO

You should not take input as a floating point. This is because floating point numbers only ever represent binary fractions, which messes with the whole idea of the challenge. You can take input in any reasonable format which allow the representation of non-binary fractions, including any of the following

  • A native rational type.
  • The numerator and denominator of a fully reduced fraction. That is, a pair $p\in\mathbb Z$ and $q\in\mathbb N_{>0}$ such that $\gcd\left(\lvert p\rvert, q\right) = 1$.
  • The absolute value of the numerator and denominator along with a boolean representing the sign.

You may output in any of these formats as well types that only represent binary fractions.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Eliminating potential confusion (5 comments)
Eliminating potential confusion
trichoplax‭ wrote about 21 hours ago · edited about 21 hours ago

Interesting challenge. The spec seems complete to me, but I can imagine it being easy to make an incorrect assumption because "Convert" could lead to some people assuming that $f(x)$ is expressing $x$ as a binary fraction. Would it be worth stating explicitly that this is in general impossible so people have an idea of what they are getting into before they start?

trichoplax‭ wrote about 21 hours ago

(Alternatively maybe something other than "Convert" in the title, such as "Bijection from"?)

trichoplax‭ wrote about 18 hours ago

The new title more than covers it. I like it.

Olin Lathrop‭ wrote about 13 hours ago

Not really. Now it's even more unclear to me than before. What does it mean for a function to be "from rational numbers", for example. It would help if you explained what you wanted in plain English.

trichoplax‭ wrote about 12 hours ago

This is a very mathematical challenge. I wouldn't expect most people who write code to understand it, so its target audience will be people who enjoy both golfing code and mathematics.

The previous title sounded like a straightforward conversion challenge, which could attract people who don't have the background knowledge to attempt it. The new title sounds unapproachable to people not familiar with the mathematics, which gives a more accurate impression of the challenge.

Having said that, for people who don't yet know the terms but are interested in learning before considering trying the challenge, it might be worth making the first mention of the rational numbers a link to somewhere like Wikipedia.