νƒœκ·Έ 보관물: floating-point

floating-point

μ‹­μ§„ μ‹œκ°„ λ³€ν™˜ λ‹¨μœ„κ°€ λ°˜ν™˜λ©λ‹ˆλ‹€. λ§ˆμ°¬κ°€μ§€λ‘œ

μ†Œκ°œ

μ‹œκ°„μ΄ ν˜Όλž€ μŠ€λŸ½λ‹€. 60 μ΄ˆμ—μ„œ 1 λΆ„, 60 λΆ„μ—μ„œ 1 μ‹œκ°„, 24 μ‹œκ°„μ—μ„œ ν•˜λ£¨ (그리고 μ„±κ°€μ‹  am / pm은 말할 것도 μ—†μŠ΅λ‹ˆλ‹€!).

μš”μ¦˜μ—λŠ” 그런 어리 μ„μŒμ˜ μ—¬μ§€κ°€ μ—†μœΌλ―€λ‘œ μš°λ¦¬λŠ” μœ μΌν•˜κ²Œ 합리적인 λŒ€μ•ˆμ„ μ±„νƒν•˜κΈ°λ‘œ κ²°μ •ν–ˆμŠ΅λ‹ˆλ‹€. 즉, 맀일은 전체 λ‹¨μœ„ 1 개둜 κ°„μ£Όλ˜λ©° 더 짧은 것은 ν•΄λ‹Ή λ‚ μ§œμ˜ μ†Œμˆ˜λ‘œ ν‘œμ‹œλ©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄ β€œ12:00:00″은 β€œ0.5β€λ‘œ, β€œ01:23:45β€³λŠ” β€œ0.058159β€λ‘œ μž‘μ„± 될 수 μžˆμŠ΅λ‹ˆλ‹€.

μƒˆλ‘œμš΄ μ‹œμŠ€ν…œμ— μ΅μˆ™ν•΄μ§€λŠ” 데 μ‹œκ°„μ΄ κ±Έλ¦¬λ―€λ‘œ, μ–‘λ°©ν–₯으둜 λ³€ν™˜ ν•  μˆ˜μžˆλŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•΄μ•Όν•©λ‹ˆλ‹€.

도전

β€œhh : mm : ssβ€μ˜ ISO-8601 ν˜•μ‹μœΌλ‘œ ν˜„λŒ€μ— 제곡 ν•œ 경우 μ„ νƒν•œ μ–Έμ–΄λ‘œ ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λ©΄ λ™λ“±ν•œ 10 μ§„μˆ˜ λΆ„μˆ˜ λ‹¨μœ„κ°€ λ°˜ν™˜λ©λ‹ˆλ‹€. λ§ˆμ°¬κ°€μ§€λ‘œ μ†Œμˆ˜ 뢀뢄이 μ£Όμ–΄μ§€λ©΄ ν”„λ‘œκ·Έλž¨μ€ 처음 μ§€μ •λœ ν˜„λŒ€ ν˜•μ‹μœΌλ‘œ μ‹œκ°„μ„ λ°˜ν™˜ν•΄μ•Όν•©λ‹ˆλ‹€.

λ‹€μŒκ³Ό 같은 가정을 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

  • μ΅œμ‹  μž…λ ₯ 및 좜λ ₯ λ²”μœ„λŠ” β€œ00:00:00β€³μ—μ„œ β€œ24:00:00β€³κΉŒμ§€μž…λ‹ˆλ‹€.
  • μ†Œμˆ˜μ  μ‹œκ°„ μž…λ ₯ 및 좜λ ₯의 λ²”μœ„λŠ” β€œ0β€μ—μ„œ β€œ1β€κΉŒμ§€ κ°€λŠ₯ν•˜λ©° μ†Œμˆ˜μ  μ΄ν•˜ 5 자리 (예 : β€œ0.12345”)κΉŒμ§€ ν—ˆμš© / 좜λ ₯ ν•  수 μžˆμ–΄μ•Όν•©λ‹ˆλ‹€. 더 μ •λ°€ν•œ 것이 ν—ˆμš©λ©λ‹ˆλ‹€
  • ν”„λ‘œκ·Έλž¨μ€ μž…λ ₯을 기반으둜 μˆ˜ν–‰ ν•  λ³€ν™˜ λ°©ν–₯을 μ•Œ 수 μžˆμ–΄μ•Όν•©λ‹ˆλ‹€.
  • μ‹œκ°„ κ΄€λ ¨ κΈ°λŠ₯ / 라이브러리λ₯Ό μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€

λ‹Ήμ²¨μžλŠ” 기쀀을 λ‹¬μ„±ν•˜λŠ” κ°€μž₯ 짧은 μ½”λ“œλ‘œ κ²°μ •λ©λ‹ˆλ‹€. 그듀은 적어도 7 개의 μ‹­μ§„ 일 λ‹¨μœ„λ‘œ, λ˜λŠ” μΆ©λΆ„ν•œ 제좜이 μžˆμ—ˆμ„ λ•Œ / λ•Œμ— 선택 될 κ²ƒμž…λ‹ˆλ‹€.

예

여기에 μ˜λ„μ μœΌλ‘œ 잘λͺ» μž‘μ„±λœ JavaScript μ½”λ“œκ°€ 예제둜 μ‚¬μš©λ©λ‹ˆλ‹€.

function decimalDay(hms) {
    var x, h, m, s;
    if (typeof hms === 'string' && hms.indexOf(':') > -1) {
        x = hms.split(':');
        return (x[0] * 3600 + x[1] * 60 + x[2] * 1) / 86400;
    }
    h = Math.floor(hms * 24) % 24;
    m = Math.floor(hms * 1440) % 60;
    s = Math.floor(hms * 86400) % 60;
    return (h > 9 ? '' : '0') + h + ':' + (m > 9 ? '' : '0') + m + ':' + (s > 9 ? '' : '0') + s;
}
decimalDay('02:57:46'); // 0.12344907407407407
decimalDay('23:42:12'); // 0.9876388888888888
decimalDay(0.5); // 12:00:00
decimalDay(0.05816); // 01:23:45


λ‹΅λ³€

CJam, 58 56 42 λ°”μ΄νŠΈ

λ‚˜λŠ” 이것이 λ„ˆλ¬΄ κΈΈκ³  골프λ₯Ό 많이 ν•  수 μžˆλ‹€κ³  ν™•μ‹ ν•©λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ 여기에 μ΄ˆλ³΄μžκ°€ μžˆμŠ΅λ‹ˆλ‹€.

86400q':/:d_,({60bd\/}{~*i60b{s2Ue[}%':*}?

μ—¬κΈ°μ—μ„œ 온라인으둜 μ‚¬μš©ν•΄λ³΄μ‹­μ‹œμ˜€


λ‹΅λ³€

파이썬 2 159 150 141 + 2 = 143 λ°”μ΄νŠΈ

κ°„λ‹¨ν•œ 해결책은 μ•„λ§ˆλ„ 훨씬 짧을 수 μžˆμŠ΅λ‹ˆλ‹€. μž‘λ™ν•©λ‹ˆλ‹€.

β€œs둜 λ¬Άμ–΄μ•Όν•˜λŠ” μž…λ ₯을 μ„€λͺ…ν•˜κΈ° μœ„ν•΄ 2 λ°”μ΄νŠΈλ₯Ό μΆ”κ°€ν–ˆμŠ΅λ‹ˆλ‹€. λ˜ν•œ Sp3000은 8 μ§„μˆ˜λ₯Ό ν•΄μ„ν•˜λŠ” eval () κ΄€λ ¨ 문제λ₯Ό μ§€μ ν–ˆμœΌλ©° μ„œμ‹μ„ λ‹¨μΆ•ν•˜κ³  map ()을 μ‚¬μš©ν•˜μ—¬ 인쇄λ₯Ό μ œκ±°ν•˜λŠ” 방법을 λ³΄μ—¬μ£Όμ—ˆμŠ΅λ‹ˆλ‹€.

n=input();i=float;d=864e2
if':'in n:a,b,c=map(i,n.split(':'));o=a/24+b/1440+c/d
else:n=i(n);o=(':%02d'*3%(n*24,n*1440%60,n*d%60))[1:]
print o

μ΄κ³³μ—μ„œ ν™•μΈν•˜μ‹­μ‹œμ˜€.


λ‹΅λ³€

μžλ°” ( ES6 ) 116 110 λ°”μ΄νŠΈ

f=x=>x[0]?([h,m,s]=x.split(':'),+s+m*60+h*3600)/86400:[24,60,60].map(y=>('0'+~~(x*=y)%60).slice(-2)).join(':')


// for snippet demo:
i=prompt();
i=i==+i?+i:i; // convert decimal string to number type
alert(f(i))

λŒ“κΈ€ :

f=x=>
    x[0] ? // if x is a string (has a defined property at '0')
        ([h, m, s] = x.split(':'), // split into hours, minutes, seconds
        +s + m*60 + h*3600) // calculate number of seconds
        / 86400 // divide by seconds in a day
    : // else
        [24, 60, 60]. // array of hours, minutes, seconds
        map(y=> // map each with function
            ('0' + // prepend with string zero
                ~~(x *= y) // multiply x by y and floor it
                % 60 // get remainder
            ).slice(-2) // get last 2 digits
        ).join(':') // join resulting array with colons

λ‹΅λ³€

파이썬 3 : 143 λ°”μ΄νŠΈ

i,k,l,m=input(),60,86400,float
if'.'in i:i=m(i)*l;m=(3*':%02d'%(i/k/k,i/k%k,i%k))[1:]
else:a,b,c=map(m,i.split(':'));m=(a*k*k+b*k+c)/l
print(m)

python 2 μ†”λ£¨μ…˜κ³Ό λ™μΌν•œ λ°”μ΄νŠΈ μˆ˜μ΄μ§€λ§Œ μˆ˜ν•™μ— λŒ€ν•΄ λ‹€λ₯Έ μ ‘κ·Ό 방식을 μ·¨ν•œ 것 κ°™μŠ΅λ‹ˆλ‹€.


λ‹΅λ³€

쀄리아 152 143 142 λ°”μ΄νŠΈ

κΈ€μŽ„, λ‚˜λŠ” 골프λ₯Ό μœ„ν•΄μ„œ, β€œμ€„λ¦¬μ•ˆ (Julian)”이 μ•„λ‹Œ μ ‘κ·Ό 방식을 μ—…λ°μ΄νŠΈν–ˆλ‹€. 더 λ‚˜μ€ (κ°„κ²°ν•˜μ§€λŠ” μ•Šμ§€λ§Œ) μ ‘κ·Ό 방식은 κ°œμ • 내역을 μ°Έμ‘°ν•˜μ‹­μ‹œμ˜€.

x->(t=[3600,60,1];d=86400;typeof(x)<:String?dot(int(split(x,":")),t)/d:(x*=d;o="";for i=t q,x=xΓ·i,x%i;o*=lpad(int(q),2,0)*":"end;o[1:end-1]))

λ¬Έμžμ—΄ λ˜λŠ” 64 λΉ„νŠΈ 뢀동 μ†Œμˆ˜μ  숫자λ₯Ό ν—ˆμš©ν•˜κ³  각각 64 λΉ„νŠΈ 뢀동 μ†Œμˆ˜μ  숫자 λ˜λŠ” λ¬Έμžμ—΄μ„ λ¦¬ν„΄ν•˜λŠ” μ΄λ¦„μ—†λŠ” ν•¨μˆ˜λ₯Ό μž‘μ„±ν•©λ‹ˆλ‹€. ν˜ΈμΆœν•˜λ €λ©΄ 이름을 μ§€μ •ν•˜μ‹­μ‹œμ˜€ (예 πŸ™‚ f=x->....

μ–Έ 골프 + μ„€λͺ… :

function f(x)
    # Construct a vector of the number of seconds in an hour,
    # minute, and second
    t = [3600, 60, 1]

    # Store the number of seconds in 24 hours
    d = 86400

    # Does the type of x inherit from the type String?
    if typeof(x) <: String
        # Compute the total number of observed seconds as the
        # dot product of the time split into a vector with the
        # number of seconds in an hour, minute, and second
        s = dot(int(split(x, ":")), t)

        # Get the proportion of the day by dividing this by
        # the number of seconds in 24 hours
        s / d
    else
        # Convert x to the number of observed seconds
        x *= d

        # Initialize an output string
        o = ""

        # Loop over the number of seconds in each time unit
        for i in t
            # Set q to be the quotient and x to be the remainder
            # from x divided by i
            q, x = divrem(x, i)

            # Append q to o, padded with zeroes as necessary
            o *= lpad(int(q), 2, 0) * ":"
        end

        # o has a trailing :, so return everything up to that
        o[1:end-1]
    end
end

예 :

julia> f("23:42:12")
0.9876388888888888

julia> f(0.9876388888888888)
"23:42:12"

julia> f(f("23:42:12"))
"23:42:12"

λ‹΅λ³€

C, 137 λ°”μ΄νŠΈ

전체 C ν”„λ‘œκ·Έλž¨. stdinμ—μ„œ μž…λ ₯을 μ·¨ν•˜κ³  stdoutμ—μ„œ 좜λ ₯을 μ·¨ν•©λ‹ˆλ‹€.

main(c){float a,b;scanf("%f:%f:%d",&a,&b,&c)<3?c=a*86400,printf("%02d:%02d:%02d",c/3600,c/60%60,c%60):printf("%f",a/24+b/1440+c/86400.);}

Ungolfed 및 λŒ“κΈ€ :

int main() {
    // b is float to save a . on 1440
    float a,b;
    // c is int to implicitly cast floats
    int c;

    // If the input is hh:mm:ss it gets splitted into a, b, c
    // Three arguments are filled, so ret = 3
    // If the input is a float, it gets stored in a
    // scanf stops at the first semicolon and only fills a, so ret = 1
    int ret = scanf("%f:%f:%d", &a, &b, &c);

    if(ret < 3) {
        // Got a float, convert to time
        // c = number of seconds from 00:00:00
        c = a * 86400;
        printf("%02d:%02d:%02d", c/3600, c/60 % 60, c%60);
    }
    else {
        // a = hh, b = mm, c = ss
        // In one day there are:
        // 24 hours
        // 1440 minutes
        // 86400 seconds
        printf("%f", a/24 + b/1440 + c/86400.);
    }
}

λ‹΅λ³€

J, 85 λ°”μ΄νŠΈ

κ²°κ³Ό :

T ’12 : 00 : 00 β€˜
0.5

T 0.5
12 0 0

T ’12 : 34 : 56 β€˜
0.524259

T 0.524259
12 34 56

T=:3 :'a=.86400 if.1=#y do.>.(24 60 60#:y*a)else.a%~+/3600 60 1*".y#~#:192 24 3 end.'

총 85