JavaScript está desactivado. Para una mejor experiencia, activa JavaScript en tu navegador antes de continuar.
Estás usando un navegador desactualizado. Es posible que no muestre este u otros sitios web correctamente.
Deberías actualizar o usar un
navegador alternativo .
Programming a limit helper for dagoth in python 3.12.3
🔧 Site instability resolved. You can report double-posts and broken attachments. For bigger issues, use the
Technical Grievances thread.
🇵🇦 Nuestro primer dominio localizado está en español en
kiwifarms.pa . Our first localized domain is on Spanish on
kiwifarms.pa .
Want to keep track of this thread?
Accounts can bookmark posts, watch threads for updates, and jump back to where you stopped reading.
Create account
def number_to_fraction():
try:
num = float(input("Enter a number (%): "))
if num <= 0:
print("Please enter a positive number.")
return
# Convert percentage into "1 out of X"
x = 100 / num
print(f"{num}% ≈ 1 out of {x:.16f}")
except ValueError:
print("Invalid input. Please enter a number.")
if __name__ == "__main__":
number_to_fraction()
here you go Dagoth!
I died a little on the inside when the faces showed up.
True & Honest Fan
kiwifarms.net
I didn’t ask for python math equations now I feel like blue bird bottom right panel
I didn’t ask for python math equations now I feel like blue bird bottom right panel
Ver archivo adjunto 7901819
yeah well now you get the joy of understanding how every engineer ever feels when someone brings up the "XY problem".
czarne niebo już nad nami!
True & Honest Fan
kiwifarms.net
you asked for it!
; Argonian Voter, Kiwifarms
; 32-bit NASM
; Reads a line, parses a positive decimal (up to 6 frac digits), computes 100/num,
; outputs "<num>% ≈ 1 out of <x>\n" with 6 decimal places.
BITS 32
DEFAULT REL
global _start
section .data
prompt db "Enter a number (%): ", 0
invalid_msg db "Invalid input. Please enter a number.", 10
pos_msg db "Please enter a positive number.", 10
too_big db "Number out of supported range.", 10
approx_utf8 db 0x20,0xE2,0x89,0x88,0x20, 0 ; " ≈ " + trailing 0
onee6 dq 1000000.0
hundred dq 100.0
d_onee6 dd 1000000
section .bss
inbuf resb 128
int_var resd 1
frac_var resd 1
numbuf resq 1
xbuf resq 1
scaled_num resq 1
scaled_x resq 1
outbuf resb 512
digitbuf resb 64
section .text
_start:
; --- prompt ---
mov eax, 4
mov ebx, 1
lea ecx, [rel prompt]
mov edx, 19
int 0x80
; --- read input ---
mov eax, 3
mov ebx, 0
lea ecx, [rel inbuf]
mov edx, 127
int 0x80
cmp eax, 1
jl .invalid
; esi -> input pointer
lea esi, [rel inbuf]
; parser registers:
; EAX: temporary, EBX: int part accumulator, ECX: frac accumulator, BL/CL used as counters
xor eax, eax
xor ebx, ebx ; integer part in EBX (32-bit)
xor ecx, ecx ; fractional digits value in ECX (0..999999)
xor edx, edx ; used as temp and/or flags
xor ebp, ebp ; frac_digits count in BL (we'll use EBX low later) - use EDI for frac_count
xor edi, edi ; sign flag: 0=pos,1=neg
; --- skip leading whitespace ---
.skip_spaces:
mov al, [esi]
cmp al, 0
je .invalid
cmp al, ' '
je .inc_ptr
cmp al, 9
je .inc_ptr
jmp .check_sign
.inc_ptr:
inc esi
jmp .skip_spaces
.check_sign:
mov al, [esi]
cmp al, '-'
jne .check_plus
mov edi, 1
inc esi
jmp .parse_int
.check_plus:
cmp al, '+'
jne .parse_int
inc esi
; --- parse integer digits into EBX ---
.parse_int:
xor ebx, ebx ; int accumulator
mov byte [rel digitbuf], 0 ; clear useful scratch
mov ecx, 0 ; reuse ECX as frac accumulator later; now clear
xor edx, edx
xor esi, esi ; we'll reload pointer below, simpler approach: read from buffer again
; reset pointer to buffer start
lea esi, [rel inbuf]
; skip leading spaces and optional sign again to reach digits reliably
jmp .skip2
.skip2:
mov al, [esi]
cmp al, 0
je .invalid
cmp al, ' '
je .advance2
cmp al, 9
je .advance2
jmp .process_sign2
.advance2:
inc esi
jmp .skip2
.process_sign2:
mov al, [esi]
cmp al, '+'
je .incp2
cmp al, '-'
je .setneg2
jmp .parse_int_digits
.incp2:
inc esi
jmp .parse_int_digits
.setneg2:
mov edi, 1
inc esi
jmp .parse_int_digits
.parse_int_digits:
xor ebx, ebx ; int part
xor edx, edx ; digits_found flag (0/1)
.parse_int_loop:
mov al, [esi]
cmp al, '0'
jb .maybe_dot
cmp al, '9'
ja .maybe_dot
movzx eax, al
sub eax, '0'
; EBX = EBX * 10 + eax
imul ebx, ebx, 10
add ebx, eax
mov edx, 1 ; digits found
inc esi
jmp .parse_int_loop
.maybe_dot:
mov al, [esi]
cmp al, '.'
jne .after_int
inc esi
jmp .parse_frac
.after_int:
; if no digits found in integer and no frac later, we will treat invalid later
jmp .after_frac
; --- parse fractional digits into ECX, up to 6 digits ---
.parse_frac:
xor ecx, ecx ; frac accumulator
xor ebp, ebp ; frac_digits count in EBP (0..6)
.parse_frac_loop:
mov al, [esi]
cmp al, '0'
jb .after_frac
cmp al, '9'
ja .after_frac
movzx eax, al
sub eax, '0'
cmp ebp, 6
jae .skip_more_frac
; ECX = ECX * 10 + eax
imul ecx, ecx, 10
add ecx, eax
inc ebp
.skip_more_frac:
inc esi
jmp .parse_frac_loop
.after_frac:
; now EBX = integer part, ECX = fractional digits (not yet padded), EBP = number of frac digits
; if neither integer digits nor frac digits -> invalid
cmp ebx, 0
jne .got_number
cmp ebp, 0
jne .got_number
; else invalid
jmp .invalid
.got_number:
; if sign negative -> not positive
cmp edi, 0
je .check_zero
jmp .not_positive
.check_zero:
; if integer == 0 and fractional == 0 -> zero -> not positive
cmp ebx, 0
jne .store_parts
cmp ecx, 0
jne .store_parts
jmp .not_positive
.store_parts:
; pad fractional ECX to 6 digits: multiply by 10^(6-EBP)
mov eax, ebp
mov edx, 6
sub edx, eax ; edx = remaining multiplications (0..6)
.pad_loop:
cmp edx, 0
jz .pad_done
imul ecx, ecx, 10
dec edx
jmp .pad_loop
.pad_done:
; store integer and frac into memory
mov dword [int_var], ebx
mov dword [frac_var], ecx
; --- build double num = int + frac/1e6 using x87 ---
; fild int
fild dword [int_var] ; st0 = int
; if frac == 0 -> store int as double
mov eax, ecx
cmp eax, 0
je .store_num_from_int
; else frac exists
fild dword [frac_var] ; st0 = frac ; st1 = int
fld qword [onee6] ; st0 = 1e6 ; st1 = frac ; st2 = int
fdivp st1, st0 ; st1 = frac / 1e6 ; pop -> st0 = frac/1e6 ; st1 = int
faddp st1, st0 ; st1 = int + frac/1e6 ; pop -> st0 = num
fstp qword [numbuf] ; store num (and pop)
jmp .compute_scaled
.store_num_from_int:
; st0 = int, store as double
fstp qword [numbuf] ; store int as double
.compute_scaled:
; scaled_num = round(num * 1e6) -> store qword
fld qword [numbuf] ; st0 = num
fld qword [onee6] ; st0 = 1e6 ; st1 = num
fmulp st1, st0 ; st1 = num * 1e6 ; pop -> st0 = product
frndint
fistp qword [scaled_num] ; store 64-bit integer
; check high dword of scaled_num fits zero (we only support up to 32-bit scaled)
mov eax, dword [scaled_num+4]
test eax, eax
jnz .too_big
; --- compute x = 100.0 / num ---
fld qword [hundred] ; st0 = 100
fld qword [numbuf] ; st0 = num ; st1 = 100
fdivp st1, st0 ; st1 = 100 / num ; pop -> st0 = result
fstp qword [xbuf]
; scaled_x = round(x * 1e6)
fld qword [xbuf]
fld qword [onee6]
fmulp st1, st0
frndint
fistp qword [scaled_x]
mov eax, dword [scaled_x+4]
test eax, eax
jnz .too_big
; --- format output into outbuf ---
lea edi, [rel outbuf] ; edi = write pointer
; Format num from scaled_num (low dword)
mov eax, dword [scaled_num] ; eax = scaled_low
xor edx, edx
mov ecx, d_onee6 ; ecx = 1,000,000
mov ecx, dword [rel d_onee6]
div ecx ; eax = integer_part, edx = frac_part
; convert integer_part (eax) to ASCII in digitbuf (reverse)
lea esi, [rel digitbuf + 63] ; end
mov byte [esi], 0
cmp eax, 0
jne .conv_int_loop
dec esi
mov byte [esi], '0'
jmp .conv_int_done
.conv_int_loop:
xor edx, edx
mov ebx, 10
div ebx ; eax = eax/10 (divides edx:eax by 10; remainder in edx)
add dl, '0'
dec esi
mov [esi], dl
cmp eax, 0
jne .conv_int_loop
.conv_int_done:
; copy integer digits to outbuf
.copy_digits:
mov al, [esi]
cmp al, 0
je .after_copy_digits
mov [edi], al
inc edi
inc esi
jmp .copy_digits
.after_copy_digits:
; write '.' then six fractional digits (pad with zeros)
mov byte [edi], '.'
inc edi
; edx currently may be clobbered; recompute fraction part:
mov eax, dword [scaled_num]
xor edx, edx
mov ecx, dword [rel d_onee6]
div ecx ; eax=int, edx=frac
mov esi, edx ; esi = frac part
; produce 6 digits into digitbuf (most significant first)
mov ecx, 6
.gen_frac_digits:
xor edx, edx
mov eax, esi
mov ebx, 10
div ebx ; eax = esi/10 ; edx = rem
add dl, '0'
mov [digitbuf + ecx - 1], dl
mov esi, eax
dec ecx
jnz .gen_frac_digits
; copy 6 digits to outbuf
mov ecx, 6
mov esi, 0
.copy_frac:
mov al, [digitbuf + esi]
mov [edi], al
inc edi
inc esi
dec ecx
jnz .copy_frac
; append '%' ' ' approx ' ' "1 out of "
mov byte [edi], '%'
inc edi
mov byte [edi], ' '
inc edi
lea esi, [rel approx_utf8]
mov ecx, 4
.copy_approx:
mov al, [esi]
mov [edi], al
inc esi
inc edi
dec ecx
jnz .copy_approx
; append "1 out of "
; we'll write it literal-by-literal
mov byte [edi], '1' ; '1'
inc edi
mov byte [edi], ' '
inc edi
mov byte [edi], 'o'
inc edi
mov byte [edi], 'u'
inc edi
mov byte [edi], 't'
inc edi
mov byte [edi], ' '
inc edi
mov byte [edi], 'o'
inc edi
mov byte [edi], 'f'
inc edi
mov byte [edi], ' '
inc edi
; --- format x similarly using scaled_x ---
mov eax, dword [scaled_x]
xor edx, edx
mov ecx, dword [rel d_onee6]
div ecx ; eax = int_x, edx = frac_x
; convert int_x to ASCII
lea esi, [rel digitbuf + 63]
mov byte [esi], 0
cmp eax, 0
jne .conv_int_x_loop
dec esi
mov byte [esi], '0'
jmp .conv_int_x_done
.conv_int_x_loop:
xor edx, edx
mov ebx, 10
div ebx
add dl, '0'
dec esi
mov [esi], dl
cmp eax, 0
jne .conv_int_x_loop
.conv_int_x_done:
; copy digits
.copy_digits_x:
mov al, [esi]
cmp al, 0
je .after_copy_digits_x
mov [edi], al
inc edi
inc esi
jmp .copy_digits_x
.after_copy_digits_x:
; dot
mov byte [edi], '.'
inc edi
; recompute frac_x
mov eax, dword [scaled_x]
xor edx, edx
mov ecx, dword [rel d_onee6]
div ecx
mov esi, edx
; generate 6 digits
mov ecx, 6
.gen_frac_x:
xor edx, edx
mov eax, esi
mov ebx, 10
div ebx
add dl, '0'
mov [digitbuf + ecx - 1], dl
mov esi, eax
dec ecx
jnz .gen_frac_x
; copy them
mov ecx, 6
xor esi, esi
.copy_frac_x:
mov al, [digitbuf + esi]
mov [edi], al
inc edi
inc esi
dec ecx
jnz .copy_frac_x
; newline
mov byte [edi], 10
inc edi
; --- write outbuf to stdout ---
lea eax, [rel outbuf]
mov ebx, eax
lea ecx, [rel outbuf]
mov edx, edi
sub edx, ecx ; length = edi - outbuf
mov eax, 4
mov ebx, 1
int 0x80
; exit 0
mov eax, 1
xor ebx, ebx
int 0x80
; --- error paths ---
.invalid:
mov eax, 4
mov ebx, 1
lea ecx, [rel invalid_msg]
mov edx, 40
int 0x80
mov eax, 1
mov ebx, 1
int 0x80
.not_positive:
mov eax, 4
mov ebx, 1
lea ecx, [rel pos_msg]
mov edx, 30
int 0x80
mov eax, 1
mov ebx, 1
int 0x80
.too_big:
mov eax, 4
mov ebx, 1
lea ecx, [rel too_big]
mov edx, 36
int 0x80
mov eax, 1
mov ebx, 1
int 0x80
czarne niebo już nad nami!
True & Honest Fan
kiwifarms.net
unlike you, I have no respect for people who diss python, kill yourself troon. python is god tier
czarne niebo już nad nami!
True & Honest Fan
kiwifarms.net
unlike you, I have no respect for people who diss python, kill yourself troon. python is god tier
chr(sum(range(ord(min(str(not()))))))
I type with my moustache
kiwifarms.net
I do not recall asking for any programming aid. Go back to your swampland and keep these obtuse calculations to yourself.