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

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)

0 answers

Sign up to answer this question »