Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example does not work, map returns Proxy Object #1081

Open
geyang opened this issue Oct 23, 2023 · 1 comment
Open

Example does not work, map returns Proxy Object #1081

geyang opened this issue Oct 23, 2023 · 1 comment
Labels

Comments

@geyang
Copy link

geyang commented Oct 23, 2023

Thanks for this fantastic library!

from the example, the expected output of the following map should be:

    const array = new Float16Array([1.0, 1.1, 1.2, 1.3]);
    const hey = array.map((value) => value);
    console.log(">>>", hey)

Expected Output

>>> [ 2, 2.19921875, 2.3984375, 2.599609375 ]

Actual Output (wrong)

>>> Proxy(Float16Array) {0: 32256, 1: 32256, 2: 32256, 3: 32256, buffer: ArrayBuffer(8), byteLength: 8, byteOffset: 0, length: 4}

This, however, works:

    const array = new Float16Array([1.0, 1.1, 1.2, 1.3]);
   
    const hey = array.map((value) => console.log(">>>", value ));

Outputs: (correct)

=>>> 2
>>> 2.19921875
>>> 2.400390625
>>> 2.599609375
@petamoriken
Copy link
Owner

petamoriken commented Oct 23, 2023

That is a correct behavior since Float16Array proxies Uint16Array by design.

Some JavaScript runtimes can handle the output of console.log, so try custom inspections.

Node.js (Bun):

import { Float16Array } from "@petamoriken/float16";
import { customInspect } from "@petamoriken/float16/inspect";

Float16Array.prototype[Symbol.for("nodejs.util.inspect.custom")] = customInspect;

Deno:

import { Float16Array } from "https://deno.land/x/float16/mod.ts";
import { customInspect } from "https://deno.land/x/float16/inspect.ts";

// deno-lint-ignore no-explicit-any
(Float16Array.prototype as any)[Symbol.for("Deno.customInspect")] = customInspect;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants