Ответ выводит длину контента?

Чудные примеры? Что относительно того, чтобы использовать гамма-функцию! С тех пор, Gamma n = (n-1)!.

OCaml: Используя Гамму

let rec gamma z =
    let pi = 4.0 *. atan 1.0 in
    if z < 0.5 then
        pi /. ((sin (pi*.z)) *. (gamma (1.0 -. z)))
    else
        let consts = [| 0.99999999999980993; 676.5203681218851; -1259.1392167224028;
                        771.32342877765313; -176.61502916214059; 12.507343278686905;
                 -0.13857109526572012; 9.9843695780195716e-6; 1.5056327351493116e-7;
                     |] 
        in
        let z = z -. 1.0 in
        let results = Array.fold_right 
                          (fun x y -> x +. y)
                          (Array.mapi 
                              (fun i x -> if i = 0 then x else x /. (z+.(float i)))
                              consts
                          )
                          0.0
        in
        let x = z +. (float (Array.length consts)) -. 1.5 in
        let final = (sqrt (2.0*.pi)) *. 
                    (x ** (z+.0.5)) *.
                    (exp (-.x)) *. result
        in
        final

let factorial_gamma n = int_of_float (gamma (float (n+1)))
8
задан Cœur 9 July 2019 в 07:02
поделиться

3 ответа

Самый простой способ - это, вероятно, обернуть его в другую реализацию OutputStream , которая пересылает все запросы на запись, но сохраняет внутренний счетчик. Тогда вы просто напишите вместо этого. Не должно быть слишком сложно реализовать - и действительно, он может быть уже.

РЕДАКТИРОВАТЬ: Просто угадав разумное имя ( CountingOutputStream ), мы получили реализацию в Apache Commons IO .

РЕДАКТИРОВАТЬ: Как отмечалось в другом месте, если это для HTTP и ваш клиент еще не выполняет буферизацию полных данных (в этом случае я бы подумал , это может решить проблему длина содержимого), у вас могут возникнуть проблемы из-за необходимости записать длину перед записью данных . В некоторых случаях вы можете обнаружить, что он будет работать до определенного размера (который буферизуется клиентом), а затем потерпит неудачу. В этом случае решения Дэвида будут подходящими.

16
ответ дан 5 December 2019 в 06:23
поделиться

The problem is that you must set the content length in the response header before you start writing any data to the output stream. So your options are:

  1. Write the data to a byte[] array using ByteOutputStream and then copy that to the response output stream once you have the size of the data. However, if you're writing large files, this obviously isn't an option.
  2. Write the data to a temp file and then copy that to the response output once you get the file size. Depending on what you're doing, this may have a performance penalty that is unacceptable.
  3. Depending on how expensive it is to generate the data in the first place, you could generate it once, and throw it away to get the count and then generate it again. Guessing that this is unlikely to be a realistic solution.
  4. Resign yourself to the fact that you won't be able to report the content length in the response header.
8
ответ дан 5 December 2019 в 06:23
поделиться

Вы можете рассмотреть возможность записи в собственный ByteArrayOutputStream и сбросить его в выходной поток ответа в самом конце.

2
ответ дан 5 December 2019 в 06:23
поделиться
Другие вопросы по тегам:

Похожие вопросы: