Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Write a version of this Racket function in C with identical behavior.
#lang racket (define t 5) (define count 0) (define mutex (make-semaphore 1)) (define turnstile (make-semaphore 0)) (define turnstile2 (make-semaphore 1)) (define ch (make-channel)) (define (make-producer name start) (λ () (let loop ([n start]) (sleep (* 0.01 (random 10))) (semaph...
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <omp.h> int main() { int jobs = 41, tid; omp_set_num_threads(5); #pragma omp parallel shared(jobs) private(tid) { tid = omp_get_thread_num(); while (jobs > 0) { ...
Transform the following Racket implementation into C#, maintaining the same output and logic.
#lang racket (define t 5) (define count 0) (define mutex (make-semaphore 1)) (define turnstile (make-semaphore 0)) (define turnstile2 (make-semaphore 1)) (define ch (make-channel)) (define (make-producer name start) (λ () (let loop ([n start]) (sleep (* 0.01 (random 10))) (semaph...
using System; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace Rosetta.CheckPointSync; public class Program { public async Task Main() { RobotBuilder robotBuilder = new RobotBuilder(); Task work = robotBuilder.BuildRobots( "Optimus Prime", "R. Gisk...
Write the same code in C++ as shown below in Racket.
#lang racket (define t 5) (define count 0) (define mutex (make-semaphore 1)) (define turnstile (make-semaphore 0)) (define turnstile2 (make-semaphore 1)) (define ch (make-channel)) (define (make-producer name start) (λ () (let loop ([n start]) (sleep (* 0.01 (random 10))) (semaph...
#include <iostream> #include <chrono> #include <atomic> #include <mutex> #include <random> #include <thread> std::mutex cout_lock; class Latch { std::atomic<int> semafor; public: Latch(int limit) : semafor(limit) {} void wait() { semafor.fetch_sub(1); while(semafor.load() > 0) ...
Change the programming language of this snippet from Racket to Java without modifying what it does.
#lang racket (define t 5) (define count 0) (define mutex (make-semaphore 1)) (define turnstile (make-semaphore 0)) (define turnstile2 (make-semaphore 1)) (define ch (make-channel)) (define (make-producer name start) (λ () (let loop ([n start]) (sleep (* 0.01 (random 10))) (semaph...
import java.util.Scanner; import java.util.Random; public class CheckpointSync{ public static void main(String[] args){ System.out.print("Enter number of workers to use: "); Scanner in = new Scanner(System.in); Worker.nWorkers = in.nextInt(); System.out.print("Enter number of tasks to complete:"); runTasks(...
Change the following Racket code into Python without altering its purpose.
#lang racket (define t 5) (define count 0) (define mutex (make-semaphore 1)) (define turnstile (make-semaphore 0)) (define turnstile2 (make-semaphore 1)) (define ch (make-channel)) (define (make-producer name start) (λ () (let loop ([n start]) (sleep (* 0.01 (random 10))) (semaph...
import threading import time import random def worker(workernum, barrier): sleeptime = random.random() print('Starting worker '+str(workernum)+" task 1, sleeptime="+str(sleeptime)) time.sleep(sleeptime) print('Exiting worker'+str(workernum)) barrier.wait() sleeptime = random.random...
Write the same algorithm in Go as shown in this Racket implementation.
#lang racket (define t 5) (define count 0) (define mutex (make-semaphore 1)) (define turnstile (make-semaphore 0)) (define turnstile2 (make-semaphore 1)) (define ch (make-channel)) (define (make-producer name start) (λ () (let loop ([n start]) (sleep (* 0.01 (random 10))) (semaph...
package main import ( "log" "math/rand" "sync" "time" ) func worker(part string) { log.Println(part, "worker begins part") time.Sleep(time.Duration(rand.Int63n(1e6))) log.Println(part, "worker completes part") wg.Done() } var ( partList = []string{"A", "B", "C", "D"} nAss...
Port the following code from Ruby to C with equivalent syntax and logic.
require 'socket' class Workshop def initialize @sockets = {} end def add child, parent = UNIXSocket.pair wid = fork do child.close @sockets.each_value { |sibling| sibling.close } Signal.trap("INT") { exit! } lo...
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <omp.h> int main() { int jobs = 41, tid; omp_set_num_threads(5); #pragma omp parallel shared(jobs) private(tid) { tid = omp_get_thread_num(); while (jobs > 0) { ...
Write the same algorithm in C# as shown in this Ruby implementation.
require 'socket' class Workshop def initialize @sockets = {} end def add child, parent = UNIXSocket.pair wid = fork do child.close @sockets.each_value { |sibling| sibling.close } Signal.trap("INT") { exit! } lo...
using System; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace Rosetta.CheckPointSync; public class Program { public async Task Main() { RobotBuilder robotBuilder = new RobotBuilder(); Task work = robotBuilder.BuildRobots( "Optimus Prime", "R. Gisk...
Maintain the same structure and functionality when rewriting this code in C++.
require 'socket' class Workshop def initialize @sockets = {} end def add child, parent = UNIXSocket.pair wid = fork do child.close @sockets.each_value { |sibling| sibling.close } Signal.trap("INT") { exit! } lo...
#include <iostream> #include <chrono> #include <atomic> #include <mutex> #include <random> #include <thread> std::mutex cout_lock; class Latch { std::atomic<int> semafor; public: Latch(int limit) : semafor(limit) {} void wait() { semafor.fetch_sub(1); while(semafor.load() > 0) ...
Port the provided Ruby code into Java while preserving the original functionality.
require 'socket' class Workshop def initialize @sockets = {} end def add child, parent = UNIXSocket.pair wid = fork do child.close @sockets.each_value { |sibling| sibling.close } Signal.trap("INT") { exit! } lo...
import java.util.Scanner; import java.util.Random; public class CheckpointSync{ public static void main(String[] args){ System.out.print("Enter number of workers to use: "); Scanner in = new Scanner(System.in); Worker.nWorkers = in.nextInt(); System.out.print("Enter number of tasks to complete:"); runTasks(...
Generate an equivalent Python version of this Ruby code.
require 'socket' class Workshop def initialize @sockets = {} end def add child, parent = UNIXSocket.pair wid = fork do child.close @sockets.each_value { |sibling| sibling.close } Signal.trap("INT") { exit! } lo...
import threading import time import random def worker(workernum, barrier): sleeptime = random.random() print('Starting worker '+str(workernum)+" task 1, sleeptime="+str(sleeptime)) time.sleep(sleeptime) print('Exiting worker'+str(workernum)) barrier.wait() sleeptime = random.random...
Translate the given Ruby code snippet into Go without altering its behavior.
require 'socket' class Workshop def initialize @sockets = {} end def add child, parent = UNIXSocket.pair wid = fork do child.close @sockets.each_value { |sibling| sibling.close } Signal.trap("INT") { exit! } lo...
package main import ( "log" "math/rand" "sync" "time" ) func worker(part string) { log.Println(part, "worker begins part") time.Sleep(time.Duration(rand.Int63n(1e6))) log.Println(part, "worker completes part") wg.Done() } var ( partList = []string{"A", "B", "C", "D"} nAss...
Preserve the algorithm and functionality while converting the code from Scala to C.
import java.util.Random val rgen = Random() var nWorkers = 0 var nTasks = 0 class Worker(private val threadID: Int) : Runnable { @Synchronized override fun run() { try { val workTime = rgen.nextInt(900) + 100L println("Worker $threadID will work for $workTime msec.") ...
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <omp.h> int main() { int jobs = 41, tid; omp_set_num_threads(5); #pragma omp parallel shared(jobs) private(tid) { tid = omp_get_thread_num(); while (jobs > 0) { ...
Please provide an equivalent version of this Scala code in C#.
import java.util.Random val rgen = Random() var nWorkers = 0 var nTasks = 0 class Worker(private val threadID: Int) : Runnable { @Synchronized override fun run() { try { val workTime = rgen.nextInt(900) + 100L println("Worker $threadID will work for $workTime msec.") ...
using System; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace Rosetta.CheckPointSync; public class Program { public async Task Main() { RobotBuilder robotBuilder = new RobotBuilder(); Task work = robotBuilder.BuildRobots( "Optimus Prime", "R. Gisk...
Maintain the same structure and functionality when rewriting this code in C++.
import java.util.Random val rgen = Random() var nWorkers = 0 var nTasks = 0 class Worker(private val threadID: Int) : Runnable { @Synchronized override fun run() { try { val workTime = rgen.nextInt(900) + 100L println("Worker $threadID will work for $workTime msec.") ...
#include <iostream> #include <chrono> #include <atomic> #include <mutex> #include <random> #include <thread> std::mutex cout_lock; class Latch { std::atomic<int> semafor; public: Latch(int limit) : semafor(limit) {} void wait() { semafor.fetch_sub(1); while(semafor.load() > 0) ...
Convert the following code from Scala to Java, ensuring the logic remains intact.
import java.util.Random val rgen = Random() var nWorkers = 0 var nTasks = 0 class Worker(private val threadID: Int) : Runnable { @Synchronized override fun run() { try { val workTime = rgen.nextInt(900) + 100L println("Worker $threadID will work for $workTime msec.") ...
import java.util.Scanner; import java.util.Random; public class CheckpointSync{ public static void main(String[] args){ System.out.print("Enter number of workers to use: "); Scanner in = new Scanner(System.in); Worker.nWorkers = in.nextInt(); System.out.print("Enter number of tasks to complete:"); runTasks(...
Convert this Scala snippet to Python and keep its semantics consistent.
import java.util.Random val rgen = Random() var nWorkers = 0 var nTasks = 0 class Worker(private val threadID: Int) : Runnable { @Synchronized override fun run() { try { val workTime = rgen.nextInt(900) + 100L println("Worker $threadID will work for $workTime msec.") ...
import threading import time import random def worker(workernum, barrier): sleeptime = random.random() print('Starting worker '+str(workernum)+" task 1, sleeptime="+str(sleeptime)) time.sleep(sleeptime) print('Exiting worker'+str(workernum)) barrier.wait() sleeptime = random.random...
Write the same code in Go as shown below in Scala.
import java.util.Random val rgen = Random() var nWorkers = 0 var nTasks = 0 class Worker(private val threadID: Int) : Runnable { @Synchronized override fun run() { try { val workTime = rgen.nextInt(900) + 100L println("Worker $threadID will work for $workTime msec.") ...
package main import ( "log" "math/rand" "sync" "time" ) func worker(part string) { log.Println(part, "worker begins part") time.Sleep(time.Duration(rand.Int63n(1e6))) log.Println(part, "worker completes part") wg.Done() } var ( partList = []string{"A", "B", "C", "D"} nAss...
Convert this Tcl snippet to C and keep its semantics consistent.
package require Tcl 8.5 package require Thread namespace eval checkpoint { namespace export {[a-z]*} namespace ensemble create variable members {} variable waiting {} variable event proc Join {id} { variable members variable counter if {$id ni $members} { lappend members $id } re...
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <omp.h> int main() { int jobs = 41, tid; omp_set_num_threads(5); #pragma omp parallel shared(jobs) private(tid) { tid = omp_get_thread_num(); while (jobs > 0) { ...
Write a version of this Tcl function in C# with identical behavior.
package require Tcl 8.5 package require Thread namespace eval checkpoint { namespace export {[a-z]*} namespace ensemble create variable members {} variable waiting {} variable event proc Join {id} { variable members variable counter if {$id ni $members} { lappend members $id } re...
using System; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace Rosetta.CheckPointSync; public class Program { public async Task Main() { RobotBuilder robotBuilder = new RobotBuilder(); Task work = robotBuilder.BuildRobots( "Optimus Prime", "R. Gisk...
Ensure the translated C++ code behaves exactly like the original Tcl snippet.
package require Tcl 8.5 package require Thread namespace eval checkpoint { namespace export {[a-z]*} namespace ensemble create variable members {} variable waiting {} variable event proc Join {id} { variable members variable counter if {$id ni $members} { lappend members $id } re...
#include <iostream> #include <chrono> #include <atomic> #include <mutex> #include <random> #include <thread> std::mutex cout_lock; class Latch { std::atomic<int> semafor; public: Latch(int limit) : semafor(limit) {} void wait() { semafor.fetch_sub(1); while(semafor.load() > 0) ...
Keep all operations the same but rewrite the snippet in Java.
package require Tcl 8.5 package require Thread namespace eval checkpoint { namespace export {[a-z]*} namespace ensemble create variable members {} variable waiting {} variable event proc Join {id} { variable members variable counter if {$id ni $members} { lappend members $id } re...
import java.util.Scanner; import java.util.Random; public class CheckpointSync{ public static void main(String[] args){ System.out.print("Enter number of workers to use: "); Scanner in = new Scanner(System.in); Worker.nWorkers = in.nextInt(); System.out.print("Enter number of tasks to complete:"); runTasks(...
Rewrite the snippet below in Python so it works the same as the original Tcl code.
package require Tcl 8.5 package require Thread namespace eval checkpoint { namespace export {[a-z]*} namespace ensemble create variable members {} variable waiting {} variable event proc Join {id} { variable members variable counter if {$id ni $members} { lappend members $id } re...
import threading import time import random def worker(workernum, barrier): sleeptime = random.random() print('Starting worker '+str(workernum)+" task 1, sleeptime="+str(sleeptime)) time.sleep(sleeptime) print('Exiting worker'+str(workernum)) barrier.wait() sleeptime = random.random...
Translate the given Tcl code snippet into Go without altering its behavior.
package require Tcl 8.5 package require Thread namespace eval checkpoint { namespace export {[a-z]*} namespace ensemble create variable members {} variable waiting {} variable event proc Join {id} { variable members variable counter if {$id ni $members} { lappend members $id } re...
package main import ( "log" "math/rand" "sync" "time" ) func worker(part string) { log.Println(part, "worker begins part") time.Sleep(time.Duration(rand.Int63n(1e6))) log.Println(part, "worker completes part") wg.Done() } var ( partList = []string{"A", "B", "C", "D"} nAss...
Produce a functionally identical Rust code for the snippet given in C++.
#include <iostream> #include <chrono> #include <atomic> #include <mutex> #include <random> #include <thread> std::mutex cout_lock; class Latch { std::atomic<int> semafor; public: Latch(int limit) : semafor(limit) {} void wait() { semafor.fetch_sub(1); while(semafor.load() > 0) ...
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::channel; use std::sync::{Arc, Barrier}; use std::thread::spawn; use array_init::array_init; pub fn checkpoint() { const NUM_TASKS: usize = 10; const NUM_ITERATIONS: u8 = 10; let barrier = Barrier::new(NUM_TASKS); let events: [A...
Keep all operations the same but rewrite the snippet in Rust.
using System; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace Rosetta.CheckPointSync; public class Program { public async Task Main() { RobotBuilder robotBuilder = new RobotBuilder(); Task work = robotBuilder.BuildRobots( "Optimus Prime", "R. Gisk...
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::channel; use std::sync::{Arc, Barrier}; use std::thread::spawn; use array_init::array_init; pub fn checkpoint() { const NUM_TASKS: usize = 10; const NUM_ITERATIONS: u8 = 10; let barrier = Barrier::new(NUM_TASKS); let events: [A...
Please provide an equivalent version of this Go code in Rust.
package main import ( "log" "math/rand" "sync" "time" ) func worker(part string) { log.Println(part, "worker begins part") time.Sleep(time.Duration(rand.Int63n(1e6))) log.Println(part, "worker completes part") wg.Done() } var ( partList = []string{"A", "B", "C", "D"} nAss...
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::channel; use std::sync::{Arc, Barrier}; use std::thread::spawn; use array_init::array_init; pub fn checkpoint() { const NUM_TASKS: usize = 10; const NUM_ITERATIONS: u8 = 10; let barrier = Barrier::new(NUM_TASKS); let events: [A...
Please provide an equivalent version of this C code in Rust.
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <omp.h> int main() { int jobs = 41, tid; omp_set_num_threads(5); #pragma omp parallel shared(jobs) private(tid) { tid = omp_get_thread_num(); while (jobs > 0) { ...
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::channel; use std::sync::{Arc, Barrier}; use std::thread::spawn; use array_init::array_init; pub fn checkpoint() { const NUM_TASKS: usize = 10; const NUM_ITERATIONS: u8 = 10; let barrier = Barrier::new(NUM_TASKS); let events: [A...
Produce a language-to-language conversion: from Java to Rust, same semantics.
import java.util.Scanner; import java.util.Random; public class CheckpointSync{ public static void main(String[] args){ System.out.print("Enter number of workers to use: "); Scanner in = new Scanner(System.in); Worker.nWorkers = in.nextInt(); System.out.print("Enter number of tasks to complete:"); runTasks(...
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::channel; use std::sync::{Arc, Barrier}; use std::thread::spawn; use array_init::array_init; pub fn checkpoint() { const NUM_TASKS: usize = 10; const NUM_ITERATIONS: u8 = 10; let barrier = Barrier::new(NUM_TASKS); let events: [A...
Please provide an equivalent version of this Rust code in Python.
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::channel; use std::sync::{Arc, Barrier}; use std::thread::spawn; use array_init::array_init; pub fn checkpoint() { const NUM_TASKS: usize = 10; const NUM_ITERATIONS: u8 = 10; let barrier = Barrier::new(NUM_TASKS); let events: [A...
import threading import time import random def worker(workernum, barrier): sleeptime = random.random() print('Starting worker '+str(workernum)+" task 1, sleeptime="+str(sleeptime)) time.sleep(sleeptime) print('Exiting worker'+str(workernum)) barrier.wait() sleeptime = random.random...
Change the following Ada code into C# without altering its purpose.
with Ada.Numerics.Generic_Complex_Types; with Ada.Text_IO.Complex_IO; procedure Complex_Operations is package Complex_Types is new Ada.Numerics.Generic_Complex_Types (Long_Float); use Complex_Types; package Complex_IO is new Ada.Text_IO.Complex_IO (Complex_Types...
namespace RosettaCode.Arithmetic.Complex { using System; using System.Numerics; internal static class Program { private static void Main() { var number = Complex.ImaginaryOne; foreach (var result in new[] { number + number, number * number, -number, 1 / number, C...
Can you help me rewrite this code in C instead of Ada, keeping it the same logically?
with Ada.Numerics.Generic_Complex_Types; with Ada.Text_IO.Complex_IO; procedure Complex_Operations is package Complex_Types is new Ada.Numerics.Generic_Complex_Types (Long_Float); use Complex_Types; package Complex_IO is new Ada.Text_IO.Complex_IO (Complex_Types...
#include <complex.h> #include <stdio.h> void cprint(double complex c) { printf("%f%+fI", creal(c), cimag(c)); } void complex_operations() { double complex a = 1.0 + 1.0I; double complex b = 3.14159 + 1.2I; double complex c; printf("\na="); cprint(a); printf("\nb="); cprint(b); c = a + b; printf("...
Rewrite the snippet below in C++ so it works the same as the original Ada code.
with Ada.Numerics.Generic_Complex_Types; with Ada.Text_IO.Complex_IO; procedure Complex_Operations is package Complex_Types is new Ada.Numerics.Generic_Complex_Types (Long_Float); use Complex_Types; package Complex_IO is new Ada.Text_IO.Complex_IO (Complex_Types...
#include <iostream> #include <complex> using std::complex; void complex_operations() { complex<double> a(1.0, 1.0); complex<double> b(3.14159, 1.25); std::cout << a + b << std::endl; std::cout << a * b << std::endl; std::cout << 1.0 / a << std::endl; std::cout << -a << std::endl; std::cou...
Convert this Ada block to Go, preserving its control flow and logic.
with Ada.Numerics.Generic_Complex_Types; with Ada.Text_IO.Complex_IO; procedure Complex_Operations is package Complex_Types is new Ada.Numerics.Generic_Complex_Types (Long_Float); use Complex_Types; package Complex_IO is new Ada.Text_IO.Complex_IO (Complex_Types...
package main import ( "fmt" "math/cmplx" ) func main() { a := 1 + 1i b := 3.14159 + 1.25i fmt.Println("a: ", a) fmt.Println("b: ", b) fmt.Println("a + b: ", a+b) fmt.Println("a * b: ", a*b) fmt.Println("-a: ", -a) fmt.Println("1 / a: ", 1/a) fmt.Println("a̅...
Write the same algorithm in Java as shown in this Ada implementation.
with Ada.Numerics.Generic_Complex_Types; with Ada.Text_IO.Complex_IO; procedure Complex_Operations is package Complex_Types is new Ada.Numerics.Generic_Complex_Types (Long_Float); use Complex_Types; package Complex_IO is new Ada.Text_IO.Complex_IO (Complex_Types...
public class Complex { public final double real; public final double imag; public Complex() { this(0, 0); } public Complex(double r, double i) { real = r; imag = i; } public Complex add(Complex b) { return new Complex(this.real + b.real, this.imag + b.imag)...
Convert this Ada block to Python, preserving its control flow and logic.
with Ada.Numerics.Generic_Complex_Types; with Ada.Text_IO.Complex_IO; procedure Complex_Operations is package Complex_Types is new Ada.Numerics.Generic_Complex_Types (Long_Float); use Complex_Types; package Complex_IO is new Ada.Text_IO.Complex_IO (Complex_Types...
>>> z1 = 1.5 + 3j >>> z2 = 1.5 + 1.5j >>> z1 + z2 (3+4.5j) >>> z1 - z2 1.5j >>> z1 * z2 (-2.25+6.75j) >>> z1 / z2 (1.5+0.5j) >>> - z1 (-1.5-3j) >>> z1.conjugate() (1.5-3j) >>> abs(z1) 3.3541019662496847 >>> z1 ** z2 (-1.1024829553277784-0.38306415117199333j) >>> z1.real 1.5 >>> z1.imag 3.0 >>>
Write the same algorithm in C as shown in this Arturo implementation.
a: to :complex [1 1] b: to :complex @[pi 1.2] print ["a:" a] print ["b:" b] print ["a + b:" a + b] print ["a * b:" a * b] print ["1 / a:" 1 / a] print ["neg a:" neg a] print ["conj a:" conj a]
#include <complex.h> #include <stdio.h> void cprint(double complex c) { printf("%f%+fI", creal(c), cimag(c)); } void complex_operations() { double complex a = 1.0 + 1.0I; double complex b = 3.14159 + 1.2I; double complex c; printf("\na="); cprint(a); printf("\nb="); cprint(b); c = a + b; printf("...
Generate an equivalent C# version of this Arturo code.
a: to :complex [1 1] b: to :complex @[pi 1.2] print ["a:" a] print ["b:" b] print ["a + b:" a + b] print ["a * b:" a * b] print ["1 / a:" 1 / a] print ["neg a:" neg a] print ["conj a:" conj a]
namespace RosettaCode.Arithmetic.Complex { using System; using System.Numerics; internal static class Program { private static void Main() { var number = Complex.ImaginaryOne; foreach (var result in new[] { number + number, number * number, -number, 1 / number, C...
Keep all operations the same but rewrite the snippet in C++.
a: to :complex [1 1] b: to :complex @[pi 1.2] print ["a:" a] print ["b:" b] print ["a + b:" a + b] print ["a * b:" a * b] print ["1 / a:" 1 / a] print ["neg a:" neg a] print ["conj a:" conj a]
#include <iostream> #include <complex> using std::complex; void complex_operations() { complex<double> a(1.0, 1.0); complex<double> b(3.14159, 1.25); std::cout << a + b << std::endl; std::cout << a * b << std::endl; std::cout << 1.0 / a << std::endl; std::cout << -a << std::endl; std::cou...
Can you help me rewrite this code in Java instead of Arturo, keeping it the same logically?
a: to :complex [1 1] b: to :complex @[pi 1.2] print ["a:" a] print ["b:" b] print ["a + b:" a + b] print ["a * b:" a * b] print ["1 / a:" 1 / a] print ["neg a:" neg a] print ["conj a:" conj a]
public class Complex { public final double real; public final double imag; public Complex() { this(0, 0); } public Complex(double r, double i) { real = r; imag = i; } public Complex add(Complex b) { return new Complex(this.real + b.real, this.imag + b.imag)...
Convert this Arturo block to Python, preserving its control flow and logic.
a: to :complex [1 1] b: to :complex @[pi 1.2] print ["a:" a] print ["b:" b] print ["a + b:" a + b] print ["a * b:" a * b] print ["1 / a:" 1 / a] print ["neg a:" neg a] print ["conj a:" conj a]
>>> z1 = 1.5 + 3j >>> z2 = 1.5 + 1.5j >>> z1 + z2 (3+4.5j) >>> z1 - z2 1.5j >>> z1 * z2 (-2.25+6.75j) >>> z1 / z2 (1.5+0.5j) >>> - z1 (-1.5-3j) >>> z1.conjugate() (1.5-3j) >>> abs(z1) 3.3541019662496847 >>> z1 ** z2 (-1.1024829553277784-0.38306415117199333j) >>> z1.real 1.5 >>> z1.imag 3.0 >>>
Write the same algorithm in Go as shown in this Arturo implementation.
a: to :complex [1 1] b: to :complex @[pi 1.2] print ["a:" a] print ["b:" b] print ["a + b:" a + b] print ["a * b:" a * b] print ["1 / a:" 1 / a] print ["neg a:" neg a] print ["conj a:" conj a]
package main import ( "fmt" "math/cmplx" ) func main() { a := 1 + 1i b := 3.14159 + 1.25i fmt.Println("a: ", a) fmt.Println("b: ", b) fmt.Println("a + b: ", a+b) fmt.Println("a * b: ", a*b) fmt.Println("-a: ", -a) fmt.Println("1 / a: ", 1/a) fmt.Println("a̅...
Can you help me rewrite this code in C instead of AutoHotKey, keeping it the same logically?
Cset(C,1,1) MsgBox % Cstr(C)   Cneg(C,C) MsgBox % Cstr(C)   Cadd(C,C,C) MsgBox % Cstr(C)   Cinv(D,C) MsgBox % Cstr(D)   Cmul(C,C,D) MsgBox % Cstr(C)   Cset(ByRef C, re, im) { VarSetCapacity(C,16) NumPut(re,C,0,"double") NumPut(im,C,8,"double") } Cre(ByRef C) { Return NumGet(C,0,"double") } Cim(ByRef C) { ...
#include <complex.h> #include <stdio.h> void cprint(double complex c) { printf("%f%+fI", creal(c), cimag(c)); } void complex_operations() { double complex a = 1.0 + 1.0I; double complex b = 3.14159 + 1.2I; double complex c; printf("\na="); cprint(a); printf("\nb="); cprint(b); c = a + b; printf("...
Convert the following code from AutoHotKey to C#, ensuring the logic remains intact.
Cset(C,1,1) MsgBox % Cstr(C)   Cneg(C,C) MsgBox % Cstr(C)   Cadd(C,C,C) MsgBox % Cstr(C)   Cinv(D,C) MsgBox % Cstr(D)   Cmul(C,C,D) MsgBox % Cstr(C)   Cset(ByRef C, re, im) { VarSetCapacity(C,16) NumPut(re,C,0,"double") NumPut(im,C,8,"double") } Cre(ByRef C) { Return NumGet(C,0,"double") } Cim(ByRef C) { ...
namespace RosettaCode.Arithmetic.Complex { using System; using System.Numerics; internal static class Program { private static void Main() { var number = Complex.ImaginaryOne; foreach (var result in new[] { number + number, number * number, -number, 1 / number, C...
Port the provided AutoHotKey code into C++ while preserving the original functionality.
Cset(C,1,1) MsgBox % Cstr(C)   Cneg(C,C) MsgBox % Cstr(C)   Cadd(C,C,C) MsgBox % Cstr(C)   Cinv(D,C) MsgBox % Cstr(D)   Cmul(C,C,D) MsgBox % Cstr(C)   Cset(ByRef C, re, im) { VarSetCapacity(C,16) NumPut(re,C,0,"double") NumPut(im,C,8,"double") } Cre(ByRef C) { Return NumGet(C,0,"double") } Cim(ByRef C) { ...
#include <iostream> #include <complex> using std::complex; void complex_operations() { complex<double> a(1.0, 1.0); complex<double> b(3.14159, 1.25); std::cout << a + b << std::endl; std::cout << a * b << std::endl; std::cout << 1.0 / a << std::endl; std::cout << -a << std::endl; std::cou...
Port the following code from AutoHotKey to Java with equivalent syntax and logic.
Cset(C,1,1) MsgBox % Cstr(C)   Cneg(C,C) MsgBox % Cstr(C)   Cadd(C,C,C) MsgBox % Cstr(C)   Cinv(D,C) MsgBox % Cstr(D)   Cmul(C,C,D) MsgBox % Cstr(C)   Cset(ByRef C, re, im) { VarSetCapacity(C,16) NumPut(re,C,0,"double") NumPut(im,C,8,"double") } Cre(ByRef C) { Return NumGet(C,0,"double") } Cim(ByRef C) { ...
public class Complex { public final double real; public final double imag; public Complex() { this(0, 0); } public Complex(double r, double i) { real = r; imag = i; } public Complex add(Complex b) { return new Complex(this.real + b.real, this.imag + b.imag)...
Transform the following AutoHotKey implementation into Python, maintaining the same output and logic.
Cset(C,1,1) MsgBox % Cstr(C)   Cneg(C,C) MsgBox % Cstr(C)   Cadd(C,C,C) MsgBox % Cstr(C)   Cinv(D,C) MsgBox % Cstr(D)   Cmul(C,C,D) MsgBox % Cstr(C)   Cset(ByRef C, re, im) { VarSetCapacity(C,16) NumPut(re,C,0,"double") NumPut(im,C,8,"double") } Cre(ByRef C) { Return NumGet(C,0,"double") } Cim(ByRef C) { ...
>>> z1 = 1.5 + 3j >>> z2 = 1.5 + 1.5j >>> z1 + z2 (3+4.5j) >>> z1 - z2 1.5j >>> z1 * z2 (-2.25+6.75j) >>> z1 / z2 (1.5+0.5j) >>> - z1 (-1.5-3j) >>> z1.conjugate() (1.5-3j) >>> abs(z1) 3.3541019662496847 >>> z1 ** z2 (-1.1024829553277784-0.38306415117199333j) >>> z1.real 1.5 >>> z1.imag 3.0 >>>
Can you help me rewrite this code in Go instead of AutoHotKey, keeping it the same logically?
Cset(C,1,1) MsgBox % Cstr(C)   Cneg(C,C) MsgBox % Cstr(C)   Cadd(C,C,C) MsgBox % Cstr(C)   Cinv(D,C) MsgBox % Cstr(D)   Cmul(C,C,D) MsgBox % Cstr(C)   Cset(ByRef C, re, im) { VarSetCapacity(C,16) NumPut(re,C,0,"double") NumPut(im,C,8,"double") } Cre(ByRef C) { Return NumGet(C,0,"double") } Cim(ByRef C) { ...
package main import ( "fmt" "math/cmplx" ) func main() { a := 1 + 1i b := 3.14159 + 1.25i fmt.Println("a: ", a) fmt.Println("b: ", b) fmt.Println("a + b: ", a+b) fmt.Println("a * b: ", a*b) fmt.Println("-a: ", -a) fmt.Println("1 / a: ", 1/a) fmt.Println("a̅...
Rewrite this program in C while keeping its functionality equivalent to the AWK version.
function complex(arr, re, im) { arr["re"] = re arr["im"] = im } function re(cmplx) { return cmplx["re"] } function im(cmplx) { return cmplx["im"] } function printComplex(cmplx) { print re(cmplx), im(cmplx) } function abs2(cmplx) { return re(cmplx) * re(cmplx) + im(cmplx) * im(cmplx) } func...
#include <complex.h> #include <stdio.h> void cprint(double complex c) { printf("%f%+fI", creal(c), cimag(c)); } void complex_operations() { double complex a = 1.0 + 1.0I; double complex b = 3.14159 + 1.2I; double complex c; printf("\na="); cprint(a); printf("\nb="); cprint(b); c = a + b; printf("...
Produce a functionally identical C# code for the snippet given in AWK.
function complex(arr, re, im) { arr["re"] = re arr["im"] = im } function re(cmplx) { return cmplx["re"] } function im(cmplx) { return cmplx["im"] } function printComplex(cmplx) { print re(cmplx), im(cmplx) } function abs2(cmplx) { return re(cmplx) * re(cmplx) + im(cmplx) * im(cmplx) } func...
namespace RosettaCode.Arithmetic.Complex { using System; using System.Numerics; internal static class Program { private static void Main() { var number = Complex.ImaginaryOne; foreach (var result in new[] { number + number, number * number, -number, 1 / number, C...
Rewrite this program in C++ while keeping its functionality equivalent to the AWK version.
function complex(arr, re, im) { arr["re"] = re arr["im"] = im } function re(cmplx) { return cmplx["re"] } function im(cmplx) { return cmplx["im"] } function printComplex(cmplx) { print re(cmplx), im(cmplx) } function abs2(cmplx) { return re(cmplx) * re(cmplx) + im(cmplx) * im(cmplx) } func...
#include <iostream> #include <complex> using std::complex; void complex_operations() { complex<double> a(1.0, 1.0); complex<double> b(3.14159, 1.25); std::cout << a + b << std::endl; std::cout << a * b << std::endl; std::cout << 1.0 / a << std::endl; std::cout << -a << std::endl; std::cou...
Rewrite this program in Java while keeping its functionality equivalent to the AWK version.
function complex(arr, re, im) { arr["re"] = re arr["im"] = im } function re(cmplx) { return cmplx["re"] } function im(cmplx) { return cmplx["im"] } function printComplex(cmplx) { print re(cmplx), im(cmplx) } function abs2(cmplx) { return re(cmplx) * re(cmplx) + im(cmplx) * im(cmplx) } func...
public class Complex { public final double real; public final double imag; public Complex() { this(0, 0); } public Complex(double r, double i) { real = r; imag = i; } public Complex add(Complex b) { return new Complex(this.real + b.real, this.imag + b.imag)...
Write the same code in Python as shown below in AWK.
function complex(arr, re, im) { arr["re"] = re arr["im"] = im } function re(cmplx) { return cmplx["re"] } function im(cmplx) { return cmplx["im"] } function printComplex(cmplx) { print re(cmplx), im(cmplx) } function abs2(cmplx) { return re(cmplx) * re(cmplx) + im(cmplx) * im(cmplx) } func...
>>> z1 = 1.5 + 3j >>> z2 = 1.5 + 1.5j >>> z1 + z2 (3+4.5j) >>> z1 - z2 1.5j >>> z1 * z2 (-2.25+6.75j) >>> z1 / z2 (1.5+0.5j) >>> - z1 (-1.5-3j) >>> z1.conjugate() (1.5-3j) >>> abs(z1) 3.3541019662496847 >>> z1 ** z2 (-1.1024829553277784-0.38306415117199333j) >>> z1.real 1.5 >>> z1.imag 3.0 >>>
Preserve the algorithm and functionality while converting the code from AWK to Go.
function complex(arr, re, im) { arr["re"] = re arr["im"] = im } function re(cmplx) { return cmplx["re"] } function im(cmplx) { return cmplx["im"] } function printComplex(cmplx) { print re(cmplx), im(cmplx) } function abs2(cmplx) { return re(cmplx) * re(cmplx) + im(cmplx) * im(cmplx) } func...
package main import ( "fmt" "math/cmplx" ) func main() { a := 1 + 1i b := 3.14159 + 1.25i fmt.Println("a: ", a) fmt.Println("b: ", b) fmt.Println("a + b: ", a+b) fmt.Println("a * b: ", a*b) fmt.Println("-a: ", -a) fmt.Println("1 / a: ", 1/a) fmt.Println("a̅...
Transform the following BBC_Basic implementation into C, maintaining the same output and logic.
DIM Complex{r, i} DIM a{} = Complex{} : a.r = 1.0 : a.i = 1.0 DIM b{} = Complex{} : b.r = PI# : b.i = 1.2 DIM o{} = Complex{} PROCcomplexadd(o{}, a{}, b{}) PRINT "Result of addition is " FNcomplexshow(o{}) PROCcomplexmul(o{}, a{}, b{}) PRINT "Result of multi...
#include <complex.h> #include <stdio.h> void cprint(double complex c) { printf("%f%+fI", creal(c), cimag(c)); } void complex_operations() { double complex a = 1.0 + 1.0I; double complex b = 3.14159 + 1.2I; double complex c; printf("\na="); cprint(a); printf("\nb="); cprint(b); c = a + b; printf("...
Port the provided BBC_Basic code into C# while preserving the original functionality.
DIM Complex{r, i} DIM a{} = Complex{} : a.r = 1.0 : a.i = 1.0 DIM b{} = Complex{} : b.r = PI# : b.i = 1.2 DIM o{} = Complex{} PROCcomplexadd(o{}, a{}, b{}) PRINT "Result of addition is " FNcomplexshow(o{}) PROCcomplexmul(o{}, a{}, b{}) PRINT "Result of multi...
namespace RosettaCode.Arithmetic.Complex { using System; using System.Numerics; internal static class Program { private static void Main() { var number = Complex.ImaginaryOne; foreach (var result in new[] { number + number, number * number, -number, 1 / number, C...
Transform the following BBC_Basic implementation into C++, maintaining the same output and logic.
DIM Complex{r, i} DIM a{} = Complex{} : a.r = 1.0 : a.i = 1.0 DIM b{} = Complex{} : b.r = PI# : b.i = 1.2 DIM o{} = Complex{} PROCcomplexadd(o{}, a{}, b{}) PRINT "Result of addition is " FNcomplexshow(o{}) PROCcomplexmul(o{}, a{}, b{}) PRINT "Result of multi...
#include <iostream> #include <complex> using std::complex; void complex_operations() { complex<double> a(1.0, 1.0); complex<double> b(3.14159, 1.25); std::cout << a + b << std::endl; std::cout << a * b << std::endl; std::cout << 1.0 / a << std::endl; std::cout << -a << std::endl; std::cou...
Can you help me rewrite this code in Java instead of BBC_Basic, keeping it the same logically?
DIM Complex{r, i} DIM a{} = Complex{} : a.r = 1.0 : a.i = 1.0 DIM b{} = Complex{} : b.r = PI# : b.i = 1.2 DIM o{} = Complex{} PROCcomplexadd(o{}, a{}, b{}) PRINT "Result of addition is " FNcomplexshow(o{}) PROCcomplexmul(o{}, a{}, b{}) PRINT "Result of multi...
public class Complex { public final double real; public final double imag; public Complex() { this(0, 0); } public Complex(double r, double i) { real = r; imag = i; } public Complex add(Complex b) { return new Complex(this.real + b.real, this.imag + b.imag)...
Transform the following BBC_Basic implementation into Python, maintaining the same output and logic.
DIM Complex{r, i} DIM a{} = Complex{} : a.r = 1.0 : a.i = 1.0 DIM b{} = Complex{} : b.r = PI# : b.i = 1.2 DIM o{} = Complex{} PROCcomplexadd(o{}, a{}, b{}) PRINT "Result of addition is " FNcomplexshow(o{}) PROCcomplexmul(o{}, a{}, b{}) PRINT "Result of multi...
>>> z1 = 1.5 + 3j >>> z2 = 1.5 + 1.5j >>> z1 + z2 (3+4.5j) >>> z1 - z2 1.5j >>> z1 * z2 (-2.25+6.75j) >>> z1 / z2 (1.5+0.5j) >>> - z1 (-1.5-3j) >>> z1.conjugate() (1.5-3j) >>> abs(z1) 3.3541019662496847 >>> z1 ** z2 (-1.1024829553277784-0.38306415117199333j) >>> z1.real 1.5 >>> z1.imag 3.0 >>>
Can you help me rewrite this code in Go instead of BBC_Basic, keeping it the same logically?
DIM Complex{r, i} DIM a{} = Complex{} : a.r = 1.0 : a.i = 1.0 DIM b{} = Complex{} : b.r = PI# : b.i = 1.2 DIM o{} = Complex{} PROCcomplexadd(o{}, a{}, b{}) PRINT "Result of addition is " FNcomplexshow(o{}) PROCcomplexmul(o{}, a{}, b{}) PRINT "Result of multi...
package main import ( "fmt" "math/cmplx" ) func main() { a := 1 + 1i b := 3.14159 + 1.25i fmt.Println("a: ", a) fmt.Println("b: ", b) fmt.Println("a + b: ", a+b) fmt.Println("a * b: ", a*b) fmt.Println("-a: ", -a) fmt.Println("1 / a: ", 1/a) fmt.Println("a̅...
Port the following code from Clojure to C with equivalent syntax and logic.
(ns rosettacode.arithmetic.cmplx (:require [clojure.algo.generic.arithmetic :as ga]) (:import [java.lang Number])) (defrecord Complex [^Number r ^Number i] Object (toString [{:keys [r i]}] (apply str (cond (zero? r) [(if (= i 1) "" i) "i"] (zero? i) [r] :else [r (if (neg? ...
#include <complex.h> #include <stdio.h> void cprint(double complex c) { printf("%f%+fI", creal(c), cimag(c)); } void complex_operations() { double complex a = 1.0 + 1.0I; double complex b = 3.14159 + 1.2I; double complex c; printf("\na="); cprint(a); printf("\nb="); cprint(b); c = a + b; printf("...
Can you help me rewrite this code in C# instead of Clojure, keeping it the same logically?
(ns rosettacode.arithmetic.cmplx (:require [clojure.algo.generic.arithmetic :as ga]) (:import [java.lang Number])) (defrecord Complex [^Number r ^Number i] Object (toString [{:keys [r i]}] (apply str (cond (zero? r) [(if (= i 1) "" i) "i"] (zero? i) [r] :else [r (if (neg? ...
namespace RosettaCode.Arithmetic.Complex { using System; using System.Numerics; internal static class Program { private static void Main() { var number = Complex.ImaginaryOne; foreach (var result in new[] { number + number, number * number, -number, 1 / number, C...
Transform the following Clojure implementation into C++, maintaining the same output and logic.
(ns rosettacode.arithmetic.cmplx (:require [clojure.algo.generic.arithmetic :as ga]) (:import [java.lang Number])) (defrecord Complex [^Number r ^Number i] Object (toString [{:keys [r i]}] (apply str (cond (zero? r) [(if (= i 1) "" i) "i"] (zero? i) [r] :else [r (if (neg? ...
#include <iostream> #include <complex> using std::complex; void complex_operations() { complex<double> a(1.0, 1.0); complex<double> b(3.14159, 1.25); std::cout << a + b << std::endl; std::cout << a * b << std::endl; std::cout << 1.0 / a << std::endl; std::cout << -a << std::endl; std::cou...
Can you help me rewrite this code in Java instead of Clojure, keeping it the same logically?
(ns rosettacode.arithmetic.cmplx (:require [clojure.algo.generic.arithmetic :as ga]) (:import [java.lang Number])) (defrecord Complex [^Number r ^Number i] Object (toString [{:keys [r i]}] (apply str (cond (zero? r) [(if (= i 1) "" i) "i"] (zero? i) [r] :else [r (if (neg? ...
public class Complex { public final double real; public final double imag; public Complex() { this(0, 0); } public Complex(double r, double i) { real = r; imag = i; } public Complex add(Complex b) { return new Complex(this.real + b.real, this.imag + b.imag)...
Convert this Clojure snippet to Python and keep its semantics consistent.
(ns rosettacode.arithmetic.cmplx (:require [clojure.algo.generic.arithmetic :as ga]) (:import [java.lang Number])) (defrecord Complex [^Number r ^Number i] Object (toString [{:keys [r i]}] (apply str (cond (zero? r) [(if (= i 1) "" i) "i"] (zero? i) [r] :else [r (if (neg? ...
>>> z1 = 1.5 + 3j >>> z2 = 1.5 + 1.5j >>> z1 + z2 (3+4.5j) >>> z1 - z2 1.5j >>> z1 * z2 (-2.25+6.75j) >>> z1 / z2 (1.5+0.5j) >>> - z1 (-1.5-3j) >>> z1.conjugate() (1.5-3j) >>> abs(z1) 3.3541019662496847 >>> z1 ** z2 (-1.1024829553277784-0.38306415117199333j) >>> z1.real 1.5 >>> z1.imag 3.0 >>>
Ensure the translated Go code behaves exactly like the original Clojure snippet.
(ns rosettacode.arithmetic.cmplx (:require [clojure.algo.generic.arithmetic :as ga]) (:import [java.lang Number])) (defrecord Complex [^Number r ^Number i] Object (toString [{:keys [r i]}] (apply str (cond (zero? r) [(if (= i 1) "" i) "i"] (zero? i) [r] :else [r (if (neg? ...
package main import ( "fmt" "math/cmplx" ) func main() { a := 1 + 1i b := 3.14159 + 1.25i fmt.Println("a: ", a) fmt.Println("b: ", b) fmt.Println("a + b: ", a+b) fmt.Println("a * b: ", a*b) fmt.Println("-a: ", -a) fmt.Println("1 / a: ", 1/a) fmt.Println("a̅...
Write the same algorithm in C as shown in this Common_Lisp implementation.
> (sqrt -1) #C(0.0 1.0) > (expt #c(0 1) 2) -1
#include <complex.h> #include <stdio.h> void cprint(double complex c) { printf("%f%+fI", creal(c), cimag(c)); } void complex_operations() { double complex a = 1.0 + 1.0I; double complex b = 3.14159 + 1.2I; double complex c; printf("\na="); cprint(a); printf("\nb="); cprint(b); c = a + b; printf("...
Convert this Common_Lisp block to C#, preserving its control flow and logic.
> (sqrt -1) #C(0.0 1.0) > (expt #c(0 1) 2) -1
namespace RosettaCode.Arithmetic.Complex { using System; using System.Numerics; internal static class Program { private static void Main() { var number = Complex.ImaginaryOne; foreach (var result in new[] { number + number, number * number, -number, 1 / number, C...
Generate an equivalent C++ version of this Common_Lisp code.
> (sqrt -1) #C(0.0 1.0) > (expt #c(0 1) 2) -1
#include <iostream> #include <complex> using std::complex; void complex_operations() { complex<double> a(1.0, 1.0); complex<double> b(3.14159, 1.25); std::cout << a + b << std::endl; std::cout << a * b << std::endl; std::cout << 1.0 / a << std::endl; std::cout << -a << std::endl; std::cou...
Transform the following Common_Lisp implementation into Java, maintaining the same output and logic.
> (sqrt -1) #C(0.0 1.0) > (expt #c(0 1) 2) -1
public class Complex { public final double real; public final double imag; public Complex() { this(0, 0); } public Complex(double r, double i) { real = r; imag = i; } public Complex add(Complex b) { return new Complex(this.real + b.real, this.imag + b.imag)...
Port the provided Common_Lisp code into Python while preserving the original functionality.
> (sqrt -1) #C(0.0 1.0) > (expt #c(0 1) 2) -1
>>> z1 = 1.5 + 3j >>> z2 = 1.5 + 1.5j >>> z1 + z2 (3+4.5j) >>> z1 - z2 1.5j >>> z1 * z2 (-2.25+6.75j) >>> z1 / z2 (1.5+0.5j) >>> - z1 (-1.5-3j) >>> z1.conjugate() (1.5-3j) >>> abs(z1) 3.3541019662496847 >>> z1 ** z2 (-1.1024829553277784-0.38306415117199333j) >>> z1.real 1.5 >>> z1.imag 3.0 >>>
Preserve the algorithm and functionality while converting the code from Common_Lisp to Go.
> (sqrt -1) #C(0.0 1.0) > (expt #c(0 1) 2) -1
package main import ( "fmt" "math/cmplx" ) func main() { a := 1 + 1i b := 3.14159 + 1.25i fmt.Println("a: ", a) fmt.Println("b: ", b) fmt.Println("a + b: ", a+b) fmt.Println("a * b: ", a*b) fmt.Println("-a: ", -a) fmt.Println("1 / a: ", 1/a) fmt.Println("a̅...
Translate the given D code snippet into C without altering its behavior.
import std.stdio, std.complex; void main() { auto x = complex(1, 1); auto y = complex(3.14159, 1.2); writeln(x + y); writeln(x * y); writeln(1.0 / x); writeln(-x); }
#include <complex.h> #include <stdio.h> void cprint(double complex c) { printf("%f%+fI", creal(c), cimag(c)); } void complex_operations() { double complex a = 1.0 + 1.0I; double complex b = 3.14159 + 1.2I; double complex c; printf("\na="); cprint(a); printf("\nb="); cprint(b); c = a + b; printf("...
Port the following code from D to C# with equivalent syntax and logic.
import std.stdio, std.complex; void main() { auto x = complex(1, 1); auto y = complex(3.14159, 1.2); writeln(x + y); writeln(x * y); writeln(1.0 / x); writeln(-x); }
namespace RosettaCode.Arithmetic.Complex { using System; using System.Numerics; internal static class Program { private static void Main() { var number = Complex.ImaginaryOne; foreach (var result in new[] { number + number, number * number, -number, 1 / number, C...
Write a version of this D function in C++ with identical behavior.
import std.stdio, std.complex; void main() { auto x = complex(1, 1); auto y = complex(3.14159, 1.2); writeln(x + y); writeln(x * y); writeln(1.0 / x); writeln(-x); }
#include <iostream> #include <complex> using std::complex; void complex_operations() { complex<double> a(1.0, 1.0); complex<double> b(3.14159, 1.25); std::cout << a + b << std::endl; std::cout << a * b << std::endl; std::cout << 1.0 / a << std::endl; std::cout << -a << std::endl; std::cou...
Transform the following D implementation into Java, maintaining the same output and logic.
import std.stdio, std.complex; void main() { auto x = complex(1, 1); auto y = complex(3.14159, 1.2); writeln(x + y); writeln(x * y); writeln(1.0 / x); writeln(-x); }
public class Complex { public final double real; public final double imag; public Complex() { this(0, 0); } public Complex(double r, double i) { real = r; imag = i; } public Complex add(Complex b) { return new Complex(this.real + b.real, this.imag + b.imag)...
Convert this D snippet to Python and keep its semantics consistent.
import std.stdio, std.complex; void main() { auto x = complex(1, 1); auto y = complex(3.14159, 1.2); writeln(x + y); writeln(x * y); writeln(1.0 / x); writeln(-x); }
>>> z1 = 1.5 + 3j >>> z2 = 1.5 + 1.5j >>> z1 + z2 (3+4.5j) >>> z1 - z2 1.5j >>> z1 * z2 (-2.25+6.75j) >>> z1 / z2 (1.5+0.5j) >>> - z1 (-1.5-3j) >>> z1.conjugate() (1.5-3j) >>> abs(z1) 3.3541019662496847 >>> z1 ** z2 (-1.1024829553277784-0.38306415117199333j) >>> z1.real 1.5 >>> z1.imag 3.0 >>>
Convert this D snippet to Go and keep its semantics consistent.
import std.stdio, std.complex; void main() { auto x = complex(1, 1); auto y = complex(3.14159, 1.2); writeln(x + y); writeln(x * y); writeln(1.0 / x); writeln(-x); }
package main import ( "fmt" "math/cmplx" ) func main() { a := 1 + 1i b := 3.14159 + 1.25i fmt.Println("a: ", a) fmt.Println("b: ", b) fmt.Println("a + b: ", a+b) fmt.Println("a * b: ", a*b) fmt.Println("-a: ", -a) fmt.Println("1 / a: ", 1/a) fmt.Println("a̅...
Generate an equivalent C version of this Delphi code.
program Arithmetic_Complex; uses System.SysUtils, System.VarCmplx; var a, b: Variant; begin a := VarComplexCreate(5, 3); b := VarComplexCreate(0.5, 6.0); writeln(format('(%s) + (%s) = %s',[a,b, a+b])); writeln(format('(%s) * (%s) = %s',[a,b, a*b])); writeln(format('-(%s) = %s',[a,- a])); writ...
#include <complex.h> #include <stdio.h> void cprint(double complex c) { printf("%f%+fI", creal(c), cimag(c)); } void complex_operations() { double complex a = 1.0 + 1.0I; double complex b = 3.14159 + 1.2I; double complex c; printf("\na="); cprint(a); printf("\nb="); cprint(b); c = a + b; printf("...
Ensure the translated C# code behaves exactly like the original Delphi snippet.
program Arithmetic_Complex; uses System.SysUtils, System.VarCmplx; var a, b: Variant; begin a := VarComplexCreate(5, 3); b := VarComplexCreate(0.5, 6.0); writeln(format('(%s) + (%s) = %s',[a,b, a+b])); writeln(format('(%s) * (%s) = %s',[a,b, a*b])); writeln(format('-(%s) = %s',[a,- a])); writ...
namespace RosettaCode.Arithmetic.Complex { using System; using System.Numerics; internal static class Program { private static void Main() { var number = Complex.ImaginaryOne; foreach (var result in new[] { number + number, number * number, -number, 1 / number, C...
Write the same code in C++ as shown below in Delphi.
program Arithmetic_Complex; uses System.SysUtils, System.VarCmplx; var a, b: Variant; begin a := VarComplexCreate(5, 3); b := VarComplexCreate(0.5, 6.0); writeln(format('(%s) + (%s) = %s',[a,b, a+b])); writeln(format('(%s) * (%s) = %s',[a,b, a*b])); writeln(format('-(%s) = %s',[a,- a])); writ...
#include <iostream> #include <complex> using std::complex; void complex_operations() { complex<double> a(1.0, 1.0); complex<double> b(3.14159, 1.25); std::cout << a + b << std::endl; std::cout << a * b << std::endl; std::cout << 1.0 / a << std::endl; std::cout << -a << std::endl; std::cou...
Produce a functionally identical Java code for the snippet given in Delphi.
program Arithmetic_Complex; uses System.SysUtils, System.VarCmplx; var a, b: Variant; begin a := VarComplexCreate(5, 3); b := VarComplexCreate(0.5, 6.0); writeln(format('(%s) + (%s) = %s',[a,b, a+b])); writeln(format('(%s) * (%s) = %s',[a,b, a*b])); writeln(format('-(%s) = %s',[a,- a])); writ...
public class Complex { public final double real; public final double imag; public Complex() { this(0, 0); } public Complex(double r, double i) { real = r; imag = i; } public Complex add(Complex b) { return new Complex(this.real + b.real, this.imag + b.imag)...
Port the provided Delphi code into Python while preserving the original functionality.
program Arithmetic_Complex; uses System.SysUtils, System.VarCmplx; var a, b: Variant; begin a := VarComplexCreate(5, 3); b := VarComplexCreate(0.5, 6.0); writeln(format('(%s) + (%s) = %s',[a,b, a+b])); writeln(format('(%s) * (%s) = %s',[a,b, a*b])); writeln(format('-(%s) = %s',[a,- a])); writ...
>>> z1 = 1.5 + 3j >>> z2 = 1.5 + 1.5j >>> z1 + z2 (3+4.5j) >>> z1 - z2 1.5j >>> z1 * z2 (-2.25+6.75j) >>> z1 / z2 (1.5+0.5j) >>> - z1 (-1.5-3j) >>> z1.conjugate() (1.5-3j) >>> abs(z1) 3.3541019662496847 >>> z1 ** z2 (-1.1024829553277784-0.38306415117199333j) >>> z1.real 1.5 >>> z1.imag 3.0 >>>
Port the following code from Delphi to Go with equivalent syntax and logic.
program Arithmetic_Complex; uses System.SysUtils, System.VarCmplx; var a, b: Variant; begin a := VarComplexCreate(5, 3); b := VarComplexCreate(0.5, 6.0); writeln(format('(%s) + (%s) = %s',[a,b, a+b])); writeln(format('(%s) * (%s) = %s',[a,b, a*b])); writeln(format('-(%s) = %s',[a,- a])); writ...
package main import ( "fmt" "math/cmplx" ) func main() { a := 1 + 1i b := 3.14159 + 1.25i fmt.Println("a: ", a) fmt.Println("b: ", b) fmt.Println("a + b: ", a+b) fmt.Println("a * b: ", a*b) fmt.Println("-a: ", -a) fmt.Println("1 / a: ", 1/a) fmt.Println("a̅...
Convert the following code from Elixir to C, ensuring the logic remains intact.
defmodule Complex do import Kernel, except: [abs: 1, div: 2] defstruct real: 0, imag: 0 def new(real, imag) do %__MODULE__{real: real, imag: imag} end def add(a, b) do {a, b} = convert(a, b) new(a.real + b.real, a.imag + b.imag) end def sub(a, b) do {a, b} = convert(a, b) n...
#include <complex.h> #include <stdio.h> void cprint(double complex c) { printf("%f%+fI", creal(c), cimag(c)); } void complex_operations() { double complex a = 1.0 + 1.0I; double complex b = 3.14159 + 1.2I; double complex c; printf("\na="); cprint(a); printf("\nb="); cprint(b); c = a + b; printf("...
Preserve the algorithm and functionality while converting the code from Elixir to C#.
defmodule Complex do import Kernel, except: [abs: 1, div: 2] defstruct real: 0, imag: 0 def new(real, imag) do %__MODULE__{real: real, imag: imag} end def add(a, b) do {a, b} = convert(a, b) new(a.real + b.real, a.imag + b.imag) end def sub(a, b) do {a, b} = convert(a, b) n...
namespace RosettaCode.Arithmetic.Complex { using System; using System.Numerics; internal static class Program { private static void Main() { var number = Complex.ImaginaryOne; foreach (var result in new[] { number + number, number * number, -number, 1 / number, C...
Rewrite the snippet below in C++ so it works the same as the original Elixir code.
defmodule Complex do import Kernel, except: [abs: 1, div: 2] defstruct real: 0, imag: 0 def new(real, imag) do %__MODULE__{real: real, imag: imag} end def add(a, b) do {a, b} = convert(a, b) new(a.real + b.real, a.imag + b.imag) end def sub(a, b) do {a, b} = convert(a, b) n...
#include <iostream> #include <complex> using std::complex; void complex_operations() { complex<double> a(1.0, 1.0); complex<double> b(3.14159, 1.25); std::cout << a + b << std::endl; std::cout << a * b << std::endl; std::cout << 1.0 / a << std::endl; std::cout << -a << std::endl; std::cou...
Keep all operations the same but rewrite the snippet in Java.
defmodule Complex do import Kernel, except: [abs: 1, div: 2] defstruct real: 0, imag: 0 def new(real, imag) do %__MODULE__{real: real, imag: imag} end def add(a, b) do {a, b} = convert(a, b) new(a.real + b.real, a.imag + b.imag) end def sub(a, b) do {a, b} = convert(a, b) n...
public class Complex { public final double real; public final double imag; public Complex() { this(0, 0); } public Complex(double r, double i) { real = r; imag = i; } public Complex add(Complex b) { return new Complex(this.real + b.real, this.imag + b.imag)...
Write the same code in Python as shown below in Elixir.
defmodule Complex do import Kernel, except: [abs: 1, div: 2] defstruct real: 0, imag: 0 def new(real, imag) do %__MODULE__{real: real, imag: imag} end def add(a, b) do {a, b} = convert(a, b) new(a.real + b.real, a.imag + b.imag) end def sub(a, b) do {a, b} = convert(a, b) n...
>>> z1 = 1.5 + 3j >>> z2 = 1.5 + 1.5j >>> z1 + z2 (3+4.5j) >>> z1 - z2 1.5j >>> z1 * z2 (-2.25+6.75j) >>> z1 / z2 (1.5+0.5j) >>> - z1 (-1.5-3j) >>> z1.conjugate() (1.5-3j) >>> abs(z1) 3.3541019662496847 >>> z1 ** z2 (-1.1024829553277784-0.38306415117199333j) >>> z1.real 1.5 >>> z1.imag 3.0 >>>
Generate a Go translation of this Elixir snippet without changing its computational steps.
defmodule Complex do import Kernel, except: [abs: 1, div: 2] defstruct real: 0, imag: 0 def new(real, imag) do %__MODULE__{real: real, imag: imag} end def add(a, b) do {a, b} = convert(a, b) new(a.real + b.real, a.imag + b.imag) end def sub(a, b) do {a, b} = convert(a, b) n...
package main import ( "fmt" "math/cmplx" ) func main() { a := 1 + 1i b := 3.14159 + 1.25i fmt.Println("a: ", a) fmt.Println("b: ", b) fmt.Println("a + b: ", a+b) fmt.Println("a * b: ", a*b) fmt.Println("-a: ", -a) fmt.Println("1 / a: ", 1/a) fmt.Println("a̅...
Convert this Erlang snippet to C and keep its semantics consistent.
-module(complex_number). -export([calculate/0]). -record(complex, {real, img}). calculate() -> A = #complex{real=1, img=3}, B = #complex{real=5, img=2}, Sum = add (A, B), print (Sum), Product = multiply (A, B), print (Product), Negation = negation (A), print (Negation...
#include <complex.h> #include <stdio.h> void cprint(double complex c) { printf("%f%+fI", creal(c), cimag(c)); } void complex_operations() { double complex a = 1.0 + 1.0I; double complex b = 3.14159 + 1.2I; double complex c; printf("\na="); cprint(a); printf("\nb="); cprint(b); c = a + b; printf("...
Preserve the algorithm and functionality while converting the code from Erlang to C#.
-module(complex_number). -export([calculate/0]). -record(complex, {real, img}). calculate() -> A = #complex{real=1, img=3}, B = #complex{real=5, img=2}, Sum = add (A, B), print (Sum), Product = multiply (A, B), print (Product), Negation = negation (A), print (Negation...
namespace RosettaCode.Arithmetic.Complex { using System; using System.Numerics; internal static class Program { private static void Main() { var number = Complex.ImaginaryOne; foreach (var result in new[] { number + number, number * number, -number, 1 / number, C...
Please provide an equivalent version of this Erlang code in C++.
-module(complex_number). -export([calculate/0]). -record(complex, {real, img}). calculate() -> A = #complex{real=1, img=3}, B = #complex{real=5, img=2}, Sum = add (A, B), print (Sum), Product = multiply (A, B), print (Product), Negation = negation (A), print (Negation...
#include <iostream> #include <complex> using std::complex; void complex_operations() { complex<double> a(1.0, 1.0); complex<double> b(3.14159, 1.25); std::cout << a + b << std::endl; std::cout << a * b << std::endl; std::cout << 1.0 / a << std::endl; std::cout << -a << std::endl; std::cou...
Convert this Erlang block to Java, preserving its control flow and logic.
-module(complex_number). -export([calculate/0]). -record(complex, {real, img}). calculate() -> A = #complex{real=1, img=3}, B = #complex{real=5, img=2}, Sum = add (A, B), print (Sum), Product = multiply (A, B), print (Product), Negation = negation (A), print (Negation...
public class Complex { public final double real; public final double imag; public Complex() { this(0, 0); } public Complex(double r, double i) { real = r; imag = i; } public Complex add(Complex b) { return new Complex(this.real + b.real, this.imag + b.imag)...
Convert this Erlang snippet to Python and keep its semantics consistent.
-module(complex_number). -export([calculate/0]). -record(complex, {real, img}). calculate() -> A = #complex{real=1, img=3}, B = #complex{real=5, img=2}, Sum = add (A, B), print (Sum), Product = multiply (A, B), print (Product), Negation = negation (A), print (Negation...
>>> z1 = 1.5 + 3j >>> z2 = 1.5 + 1.5j >>> z1 + z2 (3+4.5j) >>> z1 - z2 1.5j >>> z1 * z2 (-2.25+6.75j) >>> z1 / z2 (1.5+0.5j) >>> - z1 (-1.5-3j) >>> z1.conjugate() (1.5-3j) >>> abs(z1) 3.3541019662496847 >>> z1 ** z2 (-1.1024829553277784-0.38306415117199333j) >>> z1.real 1.5 >>> z1.imag 3.0 >>>
Rewrite the snippet below in Go so it works the same as the original Erlang code.
-module(complex_number). -export([calculate/0]). -record(complex, {real, img}). calculate() -> A = #complex{real=1, img=3}, B = #complex{real=5, img=2}, Sum = add (A, B), print (Sum), Product = multiply (A, B), print (Product), Negation = negation (A), print (Negation...
package main import ( "fmt" "math/cmplx" ) func main() { a := 1 + 1i b := 3.14159 + 1.25i fmt.Println("a: ", a) fmt.Println("b: ", b) fmt.Println("a + b: ", a+b) fmt.Println("a * b: ", a*b) fmt.Println("-a: ", -a) fmt.Println("1 / a: ", 1/a) fmt.Println("a̅...
Transform the following F# implementation into C, maintaining the same output and logic.
> open Microsoft.FSharp.Math;; > let a = complex 1.0 1.0;; val a : complex = 1r+1i > let b = complex 3.14159 1.25;; val b : complex = 3.14159r+1.25i > a + b;; val it : Complex = 4.14159r+2.25i {Conjugate = 4.14159r-2.25i; ImaginaryPart = 2.25; Mag...
#include <complex.h> #include <stdio.h> void cprint(double complex c) { printf("%f%+fI", creal(c), cimag(c)); } void complex_operations() { double complex a = 1.0 + 1.0I; double complex b = 3.14159 + 1.2I; double complex c; printf("\na="); cprint(a); printf("\nb="); cprint(b); c = a + b; printf("...
Write the same algorithm in C# as shown in this F# implementation.
> open Microsoft.FSharp.Math;; > let a = complex 1.0 1.0;; val a : complex = 1r+1i > let b = complex 3.14159 1.25;; val b : complex = 3.14159r+1.25i > a + b;; val it : Complex = 4.14159r+2.25i {Conjugate = 4.14159r-2.25i; ImaginaryPart = 2.25; Mag...
namespace RosettaCode.Arithmetic.Complex { using System; using System.Numerics; internal static class Program { private static void Main() { var number = Complex.ImaginaryOne; foreach (var result in new[] { number + number, number * number, -number, 1 / number, C...
Translate the given F# code snippet into C++ without altering its behavior.
> open Microsoft.FSharp.Math;; > let a = complex 1.0 1.0;; val a : complex = 1r+1i > let b = complex 3.14159 1.25;; val b : complex = 3.14159r+1.25i > a + b;; val it : Complex = 4.14159r+2.25i {Conjugate = 4.14159r-2.25i; ImaginaryPart = 2.25; Mag...
#include <iostream> #include <complex> using std::complex; void complex_operations() { complex<double> a(1.0, 1.0); complex<double> b(3.14159, 1.25); std::cout << a + b << std::endl; std::cout << a * b << std::endl; std::cout << 1.0 / a << std::endl; std::cout << -a << std::endl; std::cou...
Produce a functionally identical Java code for the snippet given in F#.
> open Microsoft.FSharp.Math;; > let a = complex 1.0 1.0;; val a : complex = 1r+1i > let b = complex 3.14159 1.25;; val b : complex = 3.14159r+1.25i > a + b;; val it : Complex = 4.14159r+2.25i {Conjugate = 4.14159r-2.25i; ImaginaryPart = 2.25; Mag...
public class Complex { public final double real; public final double imag; public Complex() { this(0, 0); } public Complex(double r, double i) { real = r; imag = i; } public Complex add(Complex b) { return new Complex(this.real + b.real, this.imag + b.imag)...