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
−1

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

That means:

  • Different inputs give different outputs.
  • For every binary fraction $x$ there is a rational number $y$, such that $f(y)=x$.
  • For every two rational numbers $x$ and $y$, if $x > y$, then $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 (7 comments)
Eliminating potential confusion
trichoplax‭ wrote 22 days ago · edited 22 days 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 22 days ago

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

trichoplax‭ wrote 22 days ago

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

Olin Lathrop‭ wrote 22 days 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 22 days 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.

Olin Lathrop‭ wrote 21 days ago

I know what a rational number is. I don't understand what it means for a function to be "from" rational numbers. I also don't understand "order equivalence", what the relation in the first set of bullets is trying to tell us, and what the notation in the second set of bullets even means in the first place.

I would have no problem with this on the Math site, where many users probably do understand these things. However here on the Code Golf site, this is just math elitism. -1 and moving on.

WheatWizard‭ wrote 21 days ago

I split the first bullet in two, and I rewrote the second with less notation. A function is "from the rational numbers" if it takes rational numbers as its inputs (domain). Sometimes you forget that something is actually jargon (xkcd), so I reworded this. Order equivalence is that the order of the inputs is the same as the order of the outputs. I tried rewording this as well.

I don't think the ideas here are complex, but solving it is definitely more mathy than the average question here. I think that's how the open-ended-function tag generally is. It might be that this isn't the kind of thing you enjoy. Thank you for taking the time to read it and give feedback.